在Maven pom.xml配置
JUnit4.x及以下用户特别注意事项
如果你是通过mvn test来运行你的测试程序 , 请确保JMockit的依赖定义出现在JUnit的依赖之前。
<!-- 先声明jmockit的依赖 --> <dependency> <groupId>org.jmockit</groupId> <artifactId>jmockit</artifactId> <version>1.36</version> <scope>test</scope> </dependency> <!-- 再声明junit的依赖 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.9</version> <scope>test</scope> </dependency>
如果你是在Eclipse中运行/调试你的测试程序, 需要确保JMockit出现在JUnit之前。你可以提供Up,Down来调整。
JMockit Coverage配置
如果你还需要使用JMockit的代码覆盖率功能,你需要在Maven pom.xml中如下定义
<plugin> <artifactId>maven-surefire-plugin</artifactId> <configuration> <argLine>-javaagent:"${settings.localRepository}/org/jmockit/jmockit/1.36/jmockit-1.36.jar=coverage"</argLine> <disableXmlReport>false</disableXmlReport> <systemPropertyVariables> <coverage-output>html</coverage-output> <coverage-outputDir>${project.build.directory}/codecoverage-output</coverage-outputDir> <coverage-metrics>all</coverage-metrics> </systemPropertyVariables> </configuration> </plugin>
常见错误
java.lang.IllegalStateException: Native library for Attach API not available in this JRECaused by: java.lang.UnsatisfiedLinkError: no attach in java.library.path
<dependency> <groupId>org.jmockit</groupId> <artifactId>jmockit</artifactId> <version>1.36</version> <scope>test</scope> </dependency>
遇到这种情况,你是用JRE来跑测试程序的,需要切换成JDK才可以。