maven-surefire-plugin是maven执行单元测试的插件,不显性配置也可以直接使用。surefire:test命令会默认绑定maven执行的test阶段。target/surefire-reports目录下会生成txt和xml两种格式的结果,不利于直观展示,需要结合其它插件一起使用。如果你自己声明了,那么可以指定自己的版本,并且可以配置自定义的参数。
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-pluginartifactId>
<configuration>
<skipTests>falseskipTests>
<testFailureIgnore>truetestFailureIgnore>
<argLine>${argLine}argLine>
configuration>
plugin>
- argLine参数说明
- jacoco:report-aggregate聚合报告后,覆盖率显示为0,与这个参数有关
- jacoco在prepare-agent阶段会生成一个属性指向jacoco的runtime agent,默认这个属性叫argLine
- 需要在maven-surefire-plugin的配置中,引用这个属性
为单元测试生成html报告,默认位置target/site/surefire-report.html
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-surefire-report-pluginartifactId>
<version>${maven-surefire-report-plugin.version}version>
<executions>
<execution>
<id>reportid>
<phase>testphase>
<goals>
<goal>reportgoal>
goals>
execution>
executions>
plugin>
执行mvn test时,会生成报告

用于生成代码覆盖率报告,可以帮助我们了解代码中哪些部分已经被测试覆盖,哪些部分需要更多的测试,默认位置target/site/jacoco/index.html
<plugin>
<groupId>org.jacocogroupId>
<artifactId>jacoco-maven-pluginartifactId>
<executions>
<execution>
<goals>
<goal>prepare-agentgoal>
goals>
<configuration>
<propertyName>argLinepropertyName>
configuration>
execution>
<execution>
<id>reportid>
<phase>testphase>
<goals>
<goal>reportgoal>
goals>
execution>
executions>
<configuration>
<append>falseappend>
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLEelement>
<limits>
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHODcounter>
<value>COVEREDRATIOvalue>
<minimum>0.80minimum>
limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>INSTRUCTIONcounter>
<value>COVEREDRATIOvalue>
<minimum>0.80minimum>
limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>LINEcounter>
<value>COVEREDRATIOvalue>
<minimum>0.80minimum>
limit>
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASScounter>
<value>MISSEDCOUNTvalue>
<maximum>0maximum>
limit>
limits>
rule>
rules>
configuration>
plugin>

如果覆盖率显示为0,需要修改maven-surefire-plugin插件的配置
