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 c7b56e0

Browse files
committedMay 16, 2021
Add method name check for generic methods Expectations
1 parent 5c6a2df commit c7b56e0

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed
 

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

+7-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
}
@@ -137,6 +137,12 @@ private boolean isMatchingMethod(@Nonnull String invokedMethod) {
137137
return isReturnTypeOfRecordedMethodAssignableToReturnTypeOfInvokedMethod(invokedMethod, returnTypeStartPos);
138138
}
139139

140+
private boolean isMatchingMethodName(@Nonnull String invokedMethod) {
141+
int methodNameEndPos = invokedMethod.indexOf('(');
142+
String methodName = invokedMethod.substring(0, methodNameEndPos + 1);
143+
return getMethodNameAndDescription().startsWith(methodName);
144+
}
145+
140146
// Returns -1 if the method names or parameters are different.
141147
private int getReturnTypePosition(@Nonnull String invokedMethod) {
142148
String recordedMethod = getMethodNameAndDescription();

‎main/test/mockit/CapturingImplementationsTest.java

+48
Original file line numberDiff line numberDiff line change
@@ -196,22 +196,26 @@ public void captureClassWhichImplementsCapturedBaseInterfaceAndExtendsUnrelatedB
196196
static class Base<T> {
197197
T doSomething() { return null; }
198198
void doSomething(T t) { System.out.println("test");}
199+
T doSomethingReturn(T t) { return t;}
199200
}
200201

201202
static final class Impl extends Base<Integer> {
202203
@Override Integer doSomething() { return 1; }
203204
@Override void doSomething(Integer i) {}
205+
@Override Integer doSomethingReturn(Integer t) { return null;}
204206
}
205207

206208
@Test
207209
public void captureImplementationsOfGenericType(@Capturing final Base<Integer> anyInstance) {
208210
new Expectations() {{
209211
anyInstance.doSomething(); result = 2;
212+
anyInstance.doSomethingReturn(0);
210213
anyInstance.doSomething(0);
211214
}};
212215

213216
Base<Integer> impl = new Impl();
214217
int i = impl.doSomething();
218+
impl.doSomethingReturn(0);
215219
impl.doSomething(0);
216220

217221
assertEquals(2, i);
@@ -263,4 +267,48 @@ public void captureLibraryClassImplementingInterfaceFromAnotherLibrary(@Capturin
263267

264268
new Verifications() {{ mock.contextInitialized(null); }};
265269
}
270+
271+
static class BaseGenericReturnTypes {
272+
Class<?> methodOne() {return null;}
273+
Class<?> methodTwo() {return null;}
274+
}
275+
static class SubGenericReturnTypes extends BaseGenericReturnTypes {}
276+
277+
@Test
278+
public void captureMethodWithGenericReturnTypes(@Capturing final BaseGenericReturnTypes mock) {
279+
new Expectations () {{
280+
mock.methodOne();
281+
result = BaseGenericReturnTypes.class;
282+
times = 1;
283+
284+
mock.methodTwo();
285+
result = SubGenericReturnTypes.class;
286+
times = 1;
287+
}};
288+
SubGenericReturnTypes subBaseGenericReturnTypes = new SubGenericReturnTypes();
289+
assertEquals(BaseGenericReturnTypes.class, subBaseGenericReturnTypes.methodOne());
290+
assertEquals(SubGenericReturnTypes.class, subBaseGenericReturnTypes.methodTwo());
291+
}
292+
293+
static class BaseR {
294+
void foo() {};
295+
void bar() {};
296+
}
297+
298+
static class SubR extends BaseR {}
299+
300+
@Test
301+
public void captureR(@Capturing final BaseR mock) {
302+
new Expectations () {{
303+
mock.foo();
304+
times = 1;
305+
306+
mock.bar();
307+
times = 1;
308+
}};
309+
SubR subR = new SubR();
310+
subR.foo();
311+
subR.bar();
312+
}
313+
266314
}

0 commit comments

Comments
 (0)
Please sign in to comment.