PowerMockito:java. lang.NoClassDefFoundError
I need to simulate a static final class in JUnit test:
private MyFinalClient client; @Before public void setup() { // set up mock client = powermockito.mock(MyFinalClient.class); service = new PatientLocationService(); service.setClient(client); }
When I run the test, I receive the following error message:
java.lang.NoClassDefFoundError: Could not initialize class MyFinalClient$$EnhancerByMockitoWithcglib$$7bb8ec03 at sun.reflect.GeneratedSerializationConstructorAccessor6.newInstance(UnkNown Source) at java.lang.reflect.Constructor.newInstance(Constructor.java:422) at org.objenesis.instantiator.sun.SunReflectionFactoryInstantiator.newInstance(SunReflectionFactoryInstantiator.java:45) at org.objenesis.ObjenesisBase.newInstance(ObjenesisBase.java:73) at org.mockito.internal.creation.instance.ObjenesisInstantiator.newInstance(ObjenesisInstantiator.java:14) at org.powermock.api.mockito.repackaged.ClassImposterizer.createProxy(ClassImposterizer.java:143) at org.powermock.api.mockito.repackaged.ClassImposterizer.imposterise(ClassImposterizer.java:58) at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110) at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58) at org.powermock.api.mockito.powermockito.mock(powermockito.java:143) at com.hihi.ohyeahServiceTest.setup(ohyeahServiceTest.java:53) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at org.junit.internal.runners.MethodRoadie.runBefores(MethodRoadie.java:132) at org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters(MethodRoadie.java:95) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:294) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282) at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86) at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120) at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33) at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45) at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122) at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106) at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53) at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59) at org.junit.runner.JUnitCore.run(JUnitCore.java:160) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212) at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Not quite sure why I got this... If I do this in the spring tool suite, it will not produce any errors, but will break at the command prompt and IntelliJ (in normal eclipse)
Please help!
thank you!
Edit: This is the powermock message:
<dependency> <groupId>org.powermock</groupId> <artifactId>powermock-api-mockito</artifactId> <version>1.6.2</version> <scope>test</scope> </dependency>
I am using spring boot starter test. From the classpath, I can see that I am using:
> mockito-all-1.9. 5 > mockito-core-1.10. nineteen
I imported the project through import - > existing Maven project (including POM. XML), and I did similar things in idea
Solution
I think you may miss some test class guidance Do you have such test case class annotations?
@RunWith(PowerMockRunner.class) @PrepareForTest(MyFinalClient.class)
This will give you some functionality to prepare the myfinalclient class with some bytecode operations so that it can generate simulations correctly
I also noticed that there are some strange things in JVM 8 when doing such things. When your test runs, the bytecode validator will complain You can solve this problem by adding the '- noverify' flag to your JVM args during testing