NullPointerException in java.lang.reflect.Method.getParameterTypes
See original GitHub issueTrying 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.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:27 (9 by maintainers)
Top Results From Across the Web
java.lang.reflect.Method.getParameterTypes java code ...
Best Java code snippets using java.lang.reflect.Method.getParameterTypes ... Verifies that {@code method} produces a {@link NullPointerException} or {@link ...
Read more >java.lang.reflect.Method.getParameterTypes() Method Example
lang.reflect.Method.getParameterTypes() method returns an array of Class objects that represent the formal parameter types, in declaration order, of the method ...
Read more >NullPointerException when stubbing a test method call with ...
No matter what I do, NPE is thrown on when line almost as if the real method is getting called when setting up...
Read more >Developers - NullPointerException in java.lang.reflect.Method ...
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...
Read more >Example usage for java.lang.reflect Method ... - Java2s.com
Usage ; SuppressWarnings({ "unchecked" ; Class<?>[] parameterTypes = method.getParameterTypes(); Type[] genericParameterTypes = method.getGenericParameterTypes(); ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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 :
Unfortunately,
Method
is one of the classes that Mockito relies on internally for its behavior. StubbingMethod
will therefore lead to undefined behavior. Additionally, it is advised not to mock classes you don’t own: https://github.com/mockito/mockito/wiki/How-to-write-good-tests#dont-mock-a-type-you-dont-own We are working on improving the user experience by working on aDoNotMock
feature to avoid mocking classes/methods that are known to crash Mockito internals (#1833). Therefore, I am closing this as “Infeasible”. Apologies for the uninformative exception that is thrown.