Skip to content

java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock #678

@nkoterba

Description

@nkoterba

PowerMock version 1.6.5 fails to initiate during a test due to a class that was removed in newer versions of Mockito 2.

I don't know the exact version when it started failing, but currently:

"org.powermock:powermock-api-mockito2:1.6.5"

has a dependency on Mockito 2.0.42-Beta. All works fine.

However, if Mockito is explicitly updated to newer versions (e.g. 2.0.70-Beta at time of this issue submission), PowerMock fails during initialization with the following issue:

java.lang.NoClassDefFoundError: org/mockito/MockitoAnnotations$Mock

    at org.powermock.api.extension.listener.AnnotationEnabler.getMockAnnotations(AnnotationEnabler.java:117)
    at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:73)
    at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:55)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.powermock.reflect.internal.WhiteboxImpl.performMethodInvocation(WhiteboxImpl.java:1899)
    at org.powermock.reflect.internal.WhiteboxImpl.invokeMethod(WhiteboxImpl.java:691)
    at org.powermock.reflect.Whitebox.invokeMethod(Whitebox.java:415)
    at org.powermock.modules.testng.PowerMockTestCase.injectMocks(PowerMockTestCase.java:165)
    at org.powermock.modules.testng.PowerMockTestCase.beforePowerMockTestMethod(PowerMockTestCase.java:89)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
    at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:514)
    at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:589)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:820)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1128)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:782)
    at org.testng.TestRunner.run(TestRunner.java:632)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
    at org.testng.SuiteRunner.run(SuiteRunner.java:268)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
    at org.testng.TestNG.run(TestNG.java:1064)
    at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
    at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: org.mockito.MockitoAnnotations$Mock
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 42 more

The issue occurs in Line 117 of AnnotationEnabler.java:

    @SuppressWarnings("unchecked")
    @Override
    public Class<? extends Annotation>[] getMockAnnotations() {
        return new Class[]{org.mockito.Mock.class, Mock.class, org.powermock.core.classloader.annotations.Mock.class};
    }

The issue is that Mock.class is imported as: import org.mockito.MockitoAnnotations.Mock.

Unfortunately, in later versions of Mockito2, MockitoAnnotations does not include the Mock interface found in earlier versions as it was deprecated and has since been removed.

See: https://github.com/mockito/mockito/blob/883117afbbafef0cbe8908a3d0d77e15391fe880/src/main/java/org/mockito/MockitoAnnotations.java

The easiest fix appears to be to just remove Mock.class from the array of Class[] annotations returned by the getMockAnnotations function.

In the meantime, we are staying with version 2.0.42-Beta that is brought in when including the powermock-api-mockito2 library.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions