jmockit
是一款优秀的单元测试工具框架,项目官方网址在这里。我们这里学习它的使用,工作原理。
设置maven pom依赖
<dependencies>
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<version>${jmockit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
设置javaagent变量
不清楚从什么版本后,需要设置javaagent变量。
执行单测试类时命令如下:
mvn test -Dtest=com.gson.jmockit.tutorial.domain.ProgramConstructureTest -DargLine=“-javaagent:/Users/ruchen/.m2/repository/org/jmockit/jmockit/1.46/jmockit-1.46.jar”
如果设置maven-sunfire-plugin
,则如下
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <!-- or some other version -->
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar
</argLine>
</configuration>
</plugin>
</plugins>
maven pom
<dependency>
<groupId>org.jmockit</groupId>
<artifactId>jmockit</artifactId>
<!--1.46之后不再支持mock私有方法 -->
<version>1.46</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
maven-surefire-plugin
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <!-- or some other version -->
<configuration>
<argLine>
-javaagent:"${settings.localRepository}"/org/jmockit/jmockit/${jmockit.version}/jmockit-${jmockit.version}.jar=coverage
<!-- -Dcoverage-outputDir=target/coverage-report-->
-Dcoverage-output=html
<!-- -DforkCount=0-->
</argLine>
<!-- <systemPropertyVariables>-->
<!-- <coverage-output>html</coverage-output>-->
<!-- <coverage-metrics>all</coverage-metrics>-->
<!-- <coverage-outputDir>target/coverage-report</coverage-outputDir>-->
<!-- </systemPropertyVariables>-->
</configuration>
</plugin>
实践中发现使用mvn test 且想要输出覆盖率报告文件时,要设置coverage-output=html, 因为1.46版本下不设置该变量不启用覆盖率报告功能。(待深入学习)