Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added validation for classes that override methods from super #704

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions main/test/mockit/FakedClassWithSuperClassTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,26 @@

public final class FakedClassWithSuperClassTest
{
static class BaseClass { protected int doSomething() { return 123; } }
public static class Subclass extends BaseClass { BaseClass getInstance() { return this; } }
public static class BaseClass {
protected int doSomething() { return 123; }
protected int doSomethingElse() { return 1; }
}

public static class Subclass extends BaseClass {
BaseClass getInstance() { return this; }
@Override protected int doSomethingElse() { return super.doSomethingElse() + 1; }
}

public static final class FakeForSubclass extends MockUp<Subclass> {
@Mock public int doSomething() { return 1; }
@Mock public int doSomethingElse(Invocation inv) { return inv.proceed(); }
}

@Test
public void fakeOnlyInstancesOfTheClassSpecifiedToBeFaked() {
BaseClass d = new Subclass();
assertEquals(123, d.doSomething());
assertEquals(2, d.doSomethingElse());

new FakeForSubclass();

Expand All @@ -24,21 +33,27 @@ public void fakeOnlyInstancesOfTheClassSpecifiedToBeFaked() {
assertEquals(1, new Subclass().doSomething());
assertEquals(123, new BaseClass() {}.doSomething());
assertEquals(1, new Subclass() {}.doSomething());
assertEquals(1, new BaseClass().doSomethingElse());
assertEquals(2, new Subclass().doSomethingElse());
}

@Test
public void fakeOnlyInstancesOfTheClassSpecifiedToBeFaked_usingFakeMethodBridge() {
BaseClass d = new Subclass();
assertEquals(123, d.doSomething());
assertEquals(2, d.doSomethingElse());

new MockUp<Subclass>() {
@Mock int doSomething() { return 2; }
@Mock public int doSomethingElse(Invocation inv) { return inv.proceed(); }
};

assertEquals(123, new BaseClass().doSomething());
assertEquals(2, d.doSomething());
assertEquals(2, new Subclass().doSomething());
assertEquals(123, new BaseClass() {}.doSomething());
assertEquals(2, new Subclass() {}.doSomething());
assertEquals(1, new BaseClass().doSomethingElse());
assertEquals(2, new Subclass().doSomethingElse());
}
}