Skip to content

Commit a79bb44

Browse files
committed
Add method name check for generic methods Expectations
1 parent d2871a9 commit a79bb44

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

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

+1-4
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,6 @@ 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-
}
128125
int returnTypeStartPos = getReturnTypePosition(invokedMethod);
129126

130127
if (returnTypeStartPos < 0) {
@@ -142,7 +139,7 @@ private boolean isMatchingMethod(@Nonnull String invokedMethod) {
142139

143140
private boolean isMatchingMethodName(@Nonnull String invokedMethod) {
144141
int methodNameEndPos = invokedMethod.indexOf('(');
145-
String methodName = invokedMethod.substring(0, methodNameEndPos);
142+
String methodName = invokedMethod.substring(0, methodNameEndPos + 1);
146143
return getMethodNameAndDescription().startsWith(methodName);
147144
}
148145

main/test/mockit/CapturingImplementationsTest.java

+26
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);
@@ -285,4 +289,26 @@ public void captureMethodWithGenericReturnTypes(@Capturing final BaseGenericRetu
285289
assertEquals(BaseGenericReturnTypes.class, subBaseGenericReturnTypes.methodOne());
286290
assertEquals(SubGenericReturnTypes.class, subBaseGenericReturnTypes.methodTwo());
287291
}
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+
288314
}

0 commit comments

Comments
 (0)