Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit d2871a9

Browse files
committedMay 15, 2021
Add method name check for Expectations
1 parent 5c6a2df commit d2871a9

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed
 

‎main/src/mockit/internal/expectations/invocation/ExpectedInvocation.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ private boolean isMatchingGenericMethod(@Nullable Object mock, @Nonnull String i
113113
if (mockedClass != instance.getClass()) {
114114
GenericTypeReflection typeReflection = new GenericTypeReflection(mockedClass, null);
115115
GenericSignature parsedSignature = typeReflection.parseSignature(genericSignature);
116-
return parsedSignature.satisfiesSignature(invokedMethod);
116+
return parsedSignature.satisfiesSignature(invokedMethod) && isMatchingMethodName(invokedMethod);
117117
}
118118
}
119119
}
@@ -122,6 +122,9 @@ private boolean isMatchingGenericMethod(@Nullable Object mock, @Nonnull String i
122122
}
123123

124124
private boolean isMatchingMethod(@Nonnull String invokedMethod) {
125+
if (!isMatchingMethodName(invokedMethod)) {
126+
return false;
127+
}
125128
int returnTypeStartPos = getReturnTypePosition(invokedMethod);
126129

127130
if (returnTypeStartPos < 0) {
@@ -137,6 +140,12 @@ private boolean isMatchingMethod(@Nonnull String invokedMethod) {
137140
return isReturnTypeOfRecordedMethodAssignableToReturnTypeOfInvokedMethod(invokedMethod, returnTypeStartPos);
138141
}
139142

143+
private boolean isMatchingMethodName(@Nonnull String invokedMethod) {
144+
int methodNameEndPos = invokedMethod.indexOf('(');
145+
String methodName = invokedMethod.substring(0, methodNameEndPos);
146+
return getMethodNameAndDescription().startsWith(methodName);
147+
}
148+
140149
// Returns -1 if the method names or parameters are different.
141150
private int getReturnTypePosition(@Nonnull String invokedMethod) {
142151
String recordedMethod = getMethodNameAndDescription();

‎main/test/mockit/CapturingImplementationsTest.java

+22
Original file line numberDiff line numberDiff line change
@@ -263,4 +263,26 @@ public void captureLibraryClassImplementingInterfaceFromAnotherLibrary(@Capturin
263263

264264
new Verifications() {{ mock.contextInitialized(null); }};
265265
}
266+
267+
static class BaseGenericReturnTypes {
268+
Class<?> methodOne() {return null;}
269+
Class<?> methodTwo() {return null;}
270+
}
271+
static class SubGenericReturnTypes extends BaseGenericReturnTypes {}
272+
273+
@Test
274+
public void captureMethodWithGenericReturnTypes(@Capturing final BaseGenericReturnTypes mock) {
275+
new Expectations () {{
276+
mock.methodOne();
277+
result = BaseGenericReturnTypes.class;
278+
times = 1;
279+
280+
mock.methodTwo();
281+
result = SubGenericReturnTypes.class;
282+
times = 1;
283+
}};
284+
SubGenericReturnTypes subBaseGenericReturnTypes = new SubGenericReturnTypes();
285+
assertEquals(BaseGenericReturnTypes.class, subBaseGenericReturnTypes.methodOne());
286+
assertEquals(SubGenericReturnTypes.class, subBaseGenericReturnTypes.methodTwo());
287+
}
266288
}

0 commit comments

Comments
 (0)
Please sign in to comment.