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

Add method name check for generic methods Expectations #712

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ private boolean isMatchingGenericMethod(@Nullable Object mock, @Nonnull String i
if (mockedClass != instance.getClass()) {
GenericTypeReflection typeReflection = new GenericTypeReflection(mockedClass, null);
GenericSignature parsedSignature = typeReflection.parseSignature(genericSignature);
return parsedSignature.satisfiesSignature(invokedMethod);
return parsedSignature.satisfiesSignature(invokedMethod) && isMatchingMethodName(invokedMethod);
}
}
}
Expand All @@ -137,6 +137,12 @@ private boolean isMatchingMethod(@Nonnull String invokedMethod) {
return isReturnTypeOfRecordedMethodAssignableToReturnTypeOfInvokedMethod(invokedMethod, returnTypeStartPos);
}

private boolean isMatchingMethodName(@Nonnull String invokedMethod) {
int methodNameEndPos = invokedMethod.indexOf('(');
String methodName = invokedMethod.substring(0, methodNameEndPos + 1);
return getMethodNameAndDescription().startsWith(methodName);
}

// Returns -1 if the method names or parameters are different.
private int getReturnTypePosition(@Nonnull String invokedMethod) {
String recordedMethod = getMethodNameAndDescription();
Expand Down
48 changes: 48 additions & 0 deletions main/test/mockit/CapturingImplementationsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,22 +196,26 @@ public void captureClassWhichImplementsCapturedBaseInterfaceAndExtendsUnrelatedB
static class Base<T> {
T doSomething() { return null; }
void doSomething(T t) { System.out.println("test");}
T doSomethingReturn(T t) { return t;}
}

static final class Impl extends Base<Integer> {
@Override Integer doSomething() { return 1; }
@Override void doSomething(Integer i) {}
@Override Integer doSomethingReturn(Integer t) { return null;}
}

@Test
public void captureImplementationsOfGenericType(@Capturing final Base<Integer> anyInstance) {
new Expectations() {{
anyInstance.doSomething(); result = 2;
anyInstance.doSomethingReturn(0);
anyInstance.doSomething(0);
}};

Base<Integer> impl = new Impl();
int i = impl.doSomething();
impl.doSomethingReturn(0);
impl.doSomething(0);

assertEquals(2, i);
Expand Down Expand Up @@ -263,4 +267,48 @@ public void captureLibraryClassImplementingInterfaceFromAnotherLibrary(@Capturin

new Verifications() {{ mock.contextInitialized(null); }};
}

static class BaseGenericReturnTypes {
Class<?> methodOne() {return null;}
Class<?> methodTwo() {return null;}
}
static class SubGenericReturnTypes extends BaseGenericReturnTypes {}

@Test
public void captureMethodWithGenericReturnTypes(@Capturing final BaseGenericReturnTypes mock) {
new Expectations () {{
mock.methodOne();
result = BaseGenericReturnTypes.class;
times = 1;

mock.methodTwo();
result = SubGenericReturnTypes.class;
times = 1;
}};
SubGenericReturnTypes subBaseGenericReturnTypes = new SubGenericReturnTypes();
assertEquals(BaseGenericReturnTypes.class, subBaseGenericReturnTypes.methodOne());
assertEquals(SubGenericReturnTypes.class, subBaseGenericReturnTypes.methodTwo());
}

static class BaseR {
void foo() {};
void bar() {};
}

static class SubR extends BaseR {}

@Test
public void captureR(@Capturing final BaseR mock) {
new Expectations () {{
mock.foo();
times = 1;

mock.bar();
times = 1;
}};
SubR subR = new SubR();
subR.foo();
subR.bar();
}

}