PowerMockito:java.lang.NoClassDefFoundError
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32743115/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PowerMockito: java.lang.NoClassDefFoundError
提问by Richard Hsu
I need to mock a static final class in my Junit test:
我需要在我的 Junit 测试中模拟一个静态最终类:
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 will get the following error message:
当我运行测试时,我将收到以下错误消息:
java.lang.NoClassDefFoundError: Could not initialize class MyFinalClient$$EnhancerByMockitoWithCGLIB$bb8ec03
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.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 entirely sure why I am getting this... This will not generate any error if I do this in Spring Tool Suite but will break on command prompt and in IntelliJ (in normal eclipse as well)
不完全确定为什么我会得到这个......如果我在 Spring Tool Suite 中这样做,这不会产生任何错误,但会在命令提示符和 IntelliJ 中中断(在普通 eclipse 中也是如此)
Please help!
请帮忙!
Thanks!
谢谢!
edit: Here is the powermock info:
编辑:这是powermock信息:
<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 and from class path i can see i am using:
我正在使用 spring boot starter test,从类路径我可以看到我正在使用:
- mockito-all-1.9.5
- mockito-core-1.10.19
- mockito-all-1.9.5
- mockito-core-1.10.19
I imported the project via import->existing Maven project (by including the pom.xml) where in Idea I am doing similar things as well...
我通过 import->existing Maven 项目(通过包含 pom.xml)导入了项目,在 Idea 我也在做类似的事情......
采纳答案by Richard Hsu
Just want to provide some findings, hopefully it would help others when seeing the same situation.
只是想提供一些发现,希望它可以帮助其他人看到相同的情况。
After digging deeper into it, it seems like junit class loader would have problem loading class if the class has been loaded previously. In my case, my controller test would run fist, services test would came afterwards. Running both together would cause the no class def found exception listed above. When commenting out controller test, the junit test would work without any problem. This proves that the test itself is fine.
在深入研究之后,似乎 junit 类加载器在加载类之前会出现问题,如果该类之前已经加载过。就我而言,我的控制器测试会先运行,服务测试会在之后运行。同时运行两者会导致上面列出的 no class def found 异常。注释掉控制器测试时,junit 测试可以正常工作。这证明测试本身是没问题的。
I still have not figure out a way to deal with this situation, potential way of dealing with this is to separate service and controller into 2 projects so the unit test don't run together.
我仍然没有想出处理这种情况的方法,处理这种情况的潜在方法是将服务和控制器分成 2 个项目,这样单元测试就不会一起运行。
回答by David
I think you might be missing some bootstrapping of your test class. Do you have annotations on your test case class like this?
我认为您可能缺少一些测试类的引导。你的测试用例类上有这样的注释吗?
@RunWith(PowerMockRunner.class)
@PrepareForTest(MyFinalClient.class)
That'll get powermock to prepare the MyFinalClient class with some bytecode manipulation so that it can properly generate the mock.
这将使 powermock 通过一些字节码操作来准备 MyFinalClient 类,以便它可以正确生成模拟。
I've also noticed some weirdness under JVM 8 when doing things like this where the byte code verifier will complain when your test runs. You can get around that by adding the '-noverify' flag to your jvmargs at test.
我还注意到在 JVM 8 下做这样的事情时有些奇怪,字节码验证器会在测试运行时抱怨。您可以通过在测试时向 jvmargs 添加“-noverify”标志来解决这个问题。