You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importorg.junit.jupiter.api.Test;
importorg.mockito.Mockito;
importjava.lang.reflect.Method;
/** * Unit test for simple App. */publicclassAppTest {
@Testpublicvoidtest_something() throwsException {
MethodrealMethod = getClass().getMethod("some_method");
MethodspyMethod = Mockito.spy(realMethod);
spyMethod.invoke(this); // <- NPE
}
publicvoidsome_method() {
}
}
I cannot pin point the exact cause of the NPE from the logs / stack traces. The NPE only happens when running a maven build with JDK >11. I tested with JDK12 and 15 (same result for both). However, the test runs successfully with JDK 11. Using a mock instead of a spy works on all JDKs.
Also running the test from within Eclipse (4.18.0) works when running Eclipse with JDK11 and does not work when running with JDK > 11.
Stacktrace is like this:
java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:208)
at java.base/jdk.internal.reflect.Reflection.verifyMemberAccess(Reflection.java:127)
at java.base/java.lang.reflect.AccessibleObject.slowVerifyAccess(AccessibleObject.java:734)
at java.base/java.lang.reflect.AccessibleObject.verifyAccess(AccessibleObject.java:720)
at java.base/java.lang.reflect.AccessibleObject.checkAccess(AccessibleObject.java:692)
at java.base/java.lang.reflect.Method.invoke(Method.java:556)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:554)
at de.itemis.mosig.mockito.inline.spy.bug.AppTest.test_something(AppTest.java:16)
Any thoughts on this?
The text was updated successfully, but these errors were encountered:
java.lang.reflect.Method is one of the building blocks of Mockito and therefore we can't guarantee correctness when mocking/spying such classes. Instead, we recommend using real objects. For more information, please see the full discussion in #2026 (comment) This will likely be addressed in #1833 to notify users when they run into these kind of situations.
Hi there,
I may have encountered a bug in mockito-inline with respect to spies of final classes. You may want to checkout this demo project: https://github.com/JanMosigItemis/mockito.inline.spy.bug
I have:
The following test runs into an NPE:
I cannot pin point the exact cause of the NPE from the logs / stack traces. The NPE only happens when running a maven build with JDK >11. I tested with JDK12 and 15 (same result for both). However, the test runs successfully with JDK 11. Using a mock instead of a spy works on all JDKs.
Also running the test from within Eclipse (4.18.0) works when running Eclipse with JDK11 and does not work when running with JDK > 11.
Stacktrace is like this:
Any thoughts on this?
The text was updated successfully, but these errors were encountered: