一、上pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<surefireArgLine> </surefireArgLine>
</properties>
<dependencies>
<dependency><groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>false</skip>
<testFailureIgnore>true</testFailureIgnore>
<argLine>${surefireArgLine}</argLine>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.8</version>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</destFile>
<dataFile>${project.build.directory}/coverage-reports/jacoco-ut.exec</dataFile>
<output>file</output>
<append>true</append>
</configuration>
<executions>
<execution>
<id>pre-unit-test</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<outputDirectory>${project.reporting.outputDirectory}/jacoco-ut</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
二、执行mvn install:
console最后如下:
-------------------------------------------------------
T E S T S-------------------------------------------------------
Running com.freeager.test.jacoco.AppTest
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.257 sec
Results :
Tests run: 8, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] --- jacoco-maven-plugin:0.7.8:report (post-unit-test) @ jacoco.test ---
[INFO] Loading execution data file D:\develop\java\MyProject\JacocoTest\jacoco.test\target\coverage-reports\jacoco-ut.exec
[INFO] Analyzed bundle 'jacoco.test' with 1 classes
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ jacoco.test ---
[INFO]
[INFO] --- maven-install-plugin:2.4:install (default-install) @ jacoco.test ---
[INFO] Installing D:\develop\java\MyProject\JacocoTest\jacoco.test\target\jacoco.test-0.0.1-SNAPSHOT.jar to D:\develop\java\maven_repo\com\freeager\test\jacoco\jacoco.test\0.0.1-SNAPSHOT\jacoco.test-0.0.1-SNAPSHOT.jar
[INFO] Installing D:\develop\java\MyProject\JacocoTest\jacoco.test\pom.xml to D:\develop\java\maven_repo\com\freeager\test\jacoco\jacoco.test\0.0.1-SNAPSHOT\jacoco.test-0.0.1-SNAPSHOT.pom
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 6.054 s
[INFO] Finished at: 2018-05-06T16:34:44+08:00
[INFO] ------------------------------------------------------------------------
三、查看HTML版的单元测试覆盖率报告
如图,点开index.html即可。