Junit / PowerMockito:java.lang.NoClassDefFoundError:javassist.CannotCompileException

时间:2016-02-20 10:04:40

标签: junit mockito powermockito

在运行junit test时获得以下异常:

java.lang.NoClassDefFoundError: javassist.CannotCompileException
    at java.lang.J9VMInternals.verifyImpl(Native Method)
    at java.lang.J9VMInternals.verify(J9VMInternals.java:85)
    at java.lang.J9VMInternals.initialize(J9VMInternals.java:162)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createDefaultExtraMockTransformers(AbstractTestSuiteChunkerImpl.java:257)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.chunkClass(AbstractTestSuiteChunkerImpl.java:169)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:96)
    at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>(AbstractTestSuiteChunkerImpl.java:89)
    at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>(JUnit4TestSuiteChunkerImpl.java:49)
    at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>(AbstractCommonPowerMockRunner.java:32)
    at org.powermock.modules.junit4.PowerMockRunner.<init>(PowerMockRunner.java:34)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
    at org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:31)
    at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:24)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
    at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
    at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: javassist.CannotCompileException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:434)
    at java.lang.ClassLoader.loadClassHelper(ClassLoader.java:665)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:644)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:358)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:627)
    ... 28 more

我有以下两个班级:

public class Employee {

   public static int count() {
     throw new UnsupportedOperationException();
   }

}

public class EmployeeService {

   public int getEmployeeCount() {
     return Employee.count();
   }

}

我写了下面的测试类来测试EmployeeService:

@RunWith(PowerMockRunner.class)
@PrepareForTest(Employee.class)
public class EmployeeServiceTest {

  @Test
  public void testGetEmployeeCount() {

    PowerMockito.mockStatic(Employee.class);
    PowerMockito.when(Employee.count()).thenReturn(900);

    EmployeeService employeeService = new  EmployeeService();

    Assert.assertEquals(900,employeeService.getEmployeeCount());
  }

}

用于mockito和power mockito的依赖关系如下:

  • 的Mockito-全1.10.19.jar
  • powermock-的Mockito-1.6.2-full.jar
  • objenesis-2.1.jar
  • CGLIB的节点p-2.2.2.jar

为了解决这个异常,我还将javassist-3.12.0.GA-sources.jar添加到了类路径中,但它没有帮助。

但如上所示获得异常。请帮助我。

1 个答案:

答案 0 :(得分:1)

friend类似于包含源(java文本文件),而不是编译的java类文件。使用javassist-3.12.0.GA-sources.jar进行调查并确保包含包含jar tf javassist-3.12.0.GA-sources.jar个文件的jar。