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

RETURNS_SELF: Avoids returning mock when mock type is assignable to method return type, but method return type is Object. #2687

Merged
merged 1 commit into from Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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