question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NullPointerException in java.lang.reflect.Method.getParameterTypes

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:4
  • Comments:27 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
McKrattcommented, Jan 12, 2021

@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 :

[ERROR] getCurrentCallContext_should_return  Time elapsed: 0.001 s  <<< ERROR!
java.lang.NullPointerException: Cannot invoke "[Ljava.lang.Class;.clone()" because "this.parameterTypes" is null
	at ch.mobi.jap.context.callcontext.internal.DefaultCallContextManagerTest.getCurrentCallContext_should_return(DefaultCallContextManagerTest.java:91)
0reactions
TimvdLippecommented, Nov 3, 2021

Unfortunately, Method is one of the classes that Mockito relies on internally for its behavior. Stubbing Method 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 a DoNotMock 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found