mockito: NullPointerException in java.lang.reflect.Method.getParameterTypes
Trying to upgrade Mockito from 3.4.6
(3.4.8
wasn’t published to Maven central) to anything 3.5.x
(3.5.7
included) and I get some weird NPEs, which don’t happen if I run each test on its own:
[INFO] Running core.rest.exception.mapper.NotFoundExceptionMapperTest
07:22:56.767 [main] WARN core.rest.exception.mapper.NotFoundExceptionMapper - Endpoint not found for path: http://host:8080/404
[ERROR] Tests run: 3, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.019 s <<< FAILURE! - in core.rest.exception.mapper.NotFoundExceptionMapperTest
[ERROR] testExceptionCreatesServerErrorResponseWhenEndpointWasNotMatchedAndIsServiceRequest Time elapsed: 0.012 s <<< ERROR!
java.lang.NullPointerException
at java.base/java.lang.reflect.Method.getParameterTypes(Method.java:311)
at org.mockito.internal.creation.DelegatingMethod.<init>(DelegatingMethod.java:20)
at org.mockito.internal.invocation.DefaultInvocationFactory.createMockitoMethod(DefaultInvocationFactory.java:80)
at org.mockito.internal.invocation.DefaultInvocationFactory.createInvocation(DefaultInvocationFactory.java:59)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:58)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor.doIntercept(MockMethodInterceptor.java:47)
at org.mockito.internal.creation.bytebuddy.MockMethodInterceptor$DispatcherDefaultingToRealMethod.interceptAbstract(MockMethodInterceptor.java:129)
at org.mockito.codegen.HttpHeaders$MockitoMock$2122467463.getHeaderString(Unknown Source)
at core.rest.exception.mapper.NotFoundExceptionMapper.isServiceRequest(NotFoundExceptionMapper.java:56)
at core.rest.exception.mapper.NotFoundExceptionMapper.getResponseBuilder(NotFoundExceptionMapper.java:48)
at core.rest.exception.mapper.NotFoundExceptionMapper.getResponseBuilder(NotFoundExceptionMapper.java:13)
at core.rest.exception.mapper.BaseExceptionMapper.getResponse(BaseExceptionMapper.java:33)
at core.rest.exception.mapper.BaseExceptionMapper.toResponse(BaseExceptionMapper.java:28)
at core.rest.exception.mapper.NotFoundExceptionMapperTest.testExceptionCreatesServerErrorResponseWhenEndpointWasNotMatchedAndIsServiceRequest(NotFoundExceptionMapperTest.java:43)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
Initially I thought it might be caused by the new MockedStatic
usage, but I’ve marked those classes with @Disable
and the exceptions happen anyway and the test classes that are affected weren’t using MockedStatic
anyway, so I’m not exactly sure how to investigate this further.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 4
- Comments: 34 (11 by maintainers)
@TimvdLippe In my case I’m mocking also my own classes and get the same NPE. What can we do to help you to solve this issue ?
I don’t know if it helps but in Java 15 I got this log :
A temporary workaround is to fork each test, though build times suffer somewhat.
Since mocking method causes an issue one approach to avoid it is to use reflection to get the real method instead. e.g.
I’ll try to do one next week, after I return from vacation, but, in the meantime, I’ve commented out tests one-by-one, until I discovered what triggers the problem…
One of them has a
Mockito.mock(Method.class)
. The test itself, ran by itself, works… but if the whole suite is ran, some tests that follow it get the problem described above.Removing the mocking of the
java.lang.reflect.Method
made everything green again.