我需要使用Mockito API的PowerMock进行一些JUNIT测试。测试运行良好,但是现在我无法运行该项目。我不明白,因为所有新库都在测试范围中:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.9.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<version>1.5.4</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<version>1.5.4</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- THIS IS NEEDED FOR JAVA 1.7 -->
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<scope>test</scope>
</dependency>
当我在tomcat上部署应用程序时,我会得到此例外:
Caused by: java.lang.NoClassDefFoundError: javassist/bytecode/ClassFile
at org.hibernate.ejb.packaging.AbstractJarVisitor.checkAnnotationMatching(AbstractJarVisitor.java:246)
at org.hibernate.ejb.packaging.AbstractJarVisitor.executeJavaElementFilter(AbstractJarVisitor.java:212)
at org.hibernate.ejb.packaging.AbstractJarVisitor.addElement(AbstractJarVisitor.java:173)
at org.hibernate.ejb.packaging.ExplodedJarVisitor.getClassNamesInTree(ExplodedJarVisitor.java:126)
...
Caused by: java.lang.ClassNotFoundException: javassist.bytecode.ClassFile
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1702)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:154
我不明白为什么测试范围依赖关系干扰部署。
您真的没有足够的有关POM的信息,但这看起来很可能:
javassist是休眠的一种及时依赖性(请参阅此处),在添加PowerMock之前,它由于此依赖性而将其包含在您的战争文件中。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>XXXXX</version>
</dependency>
使用您的PowerMock Trickery,您已经告诉Maven,Javassist不应通过明确的测试范围来包括:
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<scope>test</scope>
</dependency>
因此,将Javassist范围更改为runtime
,它将包含在您的战争文件中,您的ClassNotFoundException
应该消失。