我需要使用mockito api进行一些JUnit测试的powermock。测试运行正常,但现在我无法运行该项目。我不明白,因为所有新库都在测试范围内:
<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
我不明白为什么测试范围依赖会干扰部署。
答案 0 :(得分:3)
你真的没有足够的关于你的pom的信息,但这看起来很可能:
Javassist是Hibernate的传递依赖(参见here),在你添加powermock之前,由于这种依赖性,它被包含在你的WAR文件中。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>XXXXX</version>
</dependency>
使用你的powermock技巧你告诉Maven,javassist不应该包含在你的WAR文件中,只需明确地使用它来测试范围:
<dependency>
<groupId>org.javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.18.1-GA</version>
<scope>test</scope>
</dependency>
因此,将javassist范围更改为runtime
,它将包含在您的WAR文件中,ClassNotFoundException
应该消失。