Mocking static methods not working anymore since version 4.2.0
See original GitHub issueHello, recently I tried to upgrade to the latest Mockito version from 4.1.0 to 4.2.0 which resulted in a failing build. I noticed that the unit tests which used mockstatic was not passing anymore. I can share a simple code snippet which worked and does not work anymore with the latest version:
FooUtils
public final class FooUtils {
private FooUtils() {}
public static String getName() {
return "Foo";
}
}
Unit Test
import org.junit.jupiter.api.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import static org.assertj.core.api.Assertions.assertThat;
class FooTest {
@Test
void getName() {
try (MockedStatic<FooUtils> mockedFooUtils = Mockito.mockStatic(FooUtils.class, InvocationOnMock::getMock)) {
mockedFooUtils.when(FooUtils::getName).thenReturn("bar");
String name = FooUtils.getName();
assertThat(name).isEqualTo("bar");
}
}
}
The test fails and I am getting the following error:
org.mockito.exceptions.misusing.WrongTypeOfReturnValue:
Default answer returned a result with the wrong type:
Class cannot be returned by getName()
getName() should return String
The default answer of FooUtils.class that was configured on the mock is probably incorrectly implemented.
See also this project where I use mockstatic: https://github.com/Hakky54/sslcontext-kickstart The master branch is with mockito 4.1.0 which passes however this pull request where the latest version is being used fails: https://github.com/Hakky54/sslcontext-kickstart/pull/135
I am using Java 8, 11 and 17 and it fails on all of them. I am using mockito-junit-jupiter. Any idea what could be the cause? Do you think I am using the mockstatic wrong or do I need additional configuration for the latest version?
Looking forward to hear from you guys soon.
Hakan,
Issue Analytics
- State:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top GitHub Comments
Hey @Hakky54 ,
It looks to me your implementation was wrong (because you don’t return a
List
), and your attempt to fix it (casting theinvocation.getMock()
to aList
) avoids the type verification that would throw an error.And because you mock it again after (
certificateUtilsMockedStatic.when(() -> CertificateUtils.getRootCaFromAuthorityInfoAccessExtensionIfPresent(any(X509Certificate.class))) .thenReturn(Collections.emptyList());
), it overrides your previous configuration and makes the test pass. If you remove thecertificateUtilsMockedStatic.when(
part, your test should still pass. But in that case your first mock should return a validList
, otherwise you will facejava.lang.ClassCastException: class java.lang.Class cannot be cast to class java.util.List
.In the end, you could use:
Thank you for taking your time to look at this issue, and yes I totally agree with you that my test setup was not correct and adjusting it from my side fixes all the failing tests. I think we can close this issue as it is not an issue