Skip to content

Commit

Permalink
Fix RETURNS_SELF for methods with generic return type (#2687)
Browse files Browse the repository at this point in the history
Fixes #2686
  • Loading branch information
mlichtblau committed Jun 14, 2022
1 parent 14a1bb4 commit 512ee39
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
Expand Up @@ -20,7 +20,7 @@ public Object answer(InvocationOnMock invocation) throws Throwable {
Object mock = invocation.getMock();
Class<?> mockType = MockUtil.getMockHandler(mock).getMockSettings().getTypeToMock();

if (methodReturnType.isAssignableFrom(mockType)) {
if (methodReturnType.isAssignableFrom(mockType) && methodReturnType != Object.class) {
return invocation.getMock();
}

Expand Down
Expand Up @@ -73,6 +73,13 @@ public void should_not_fail_when_calling_primitive_returning_method() {
assertThat(builder.returnInt()).isEqualTo(0);
}

@Test
public void should_not_fail_when_calling_method_with_generic_return_type() {
Builder builder = mock(Builder.class, RETURNS_SELF);

assertThat(builder.returnGeneric("Generic Result")).isEqualTo(null);
}

@Test
public void use_full_builder_with_terminating_method() {
HttpBuilder builder = mock(HttpBuilder.class, RETURNS_SELF);
Expand All @@ -99,6 +106,10 @@ public void returnNothing() {}
public int returnInt() {
return 1;
}

public <T> T returnGeneric(T result) {
return result;
}
}

private static class BuilderSubClass extends Builder {
Expand Down

0 comments on commit 512ee39

Please sign in to comment.