1、在maven的插件中添加以下配置
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
<configuration>
<destFile>target/coverage-reports/jacoco-unit.exec</destFile>
<dataFile>target/coverage-reports/jacoco-unit.exec</dataFile>
<includes>
<include>**/codecover/**</include>
<!--<include>**/service/impl/*.class</include>-->
</includes>
<!-- rules里面指定覆盖规则 -->
<rules>
<rule implementation="org.jacoco.maven.RuleConfiguration">
<element>BUNDLE</element>
<limits>
<!-- 方法覆盖到50% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>METHOD</counter>
<value>COVEREDRATIO</value>
<minimum>0.50</minimum>
</limit>
<!-- 分支覆盖到50% -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>BRANCH</counter>
<value>COVEREDRATIO</value>
<minimum>0.10</minimum>
</limit>
<!-- 类覆盖到100%,不能遗失任何类 -->
<limit implementation="org.jacoco.report.check.Limit">
<counter>CLASS</counter>
<value>MISSEDCOUNT</value>
<maximum>0</maximum>
</limit>
</limits>
</rule>
</rules>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!--这个check:对代码进行检测,控制项目构建成功还是失败-->
<execution>
<id>check</id>
<goals>
<goal>check</goal>
</goals>
</execution>
<!--这个report:对代码进行检测,然后生成index.html在 target/site/index.html中可以查看检测的详细结果-->
<execution>
<id>jacoco-site</id>
<phase>package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
2、编写测试类
例如:
测试 /codecover/ 这个包下面的类(这个包下我只有一个类)
@Component
public class TestCodeCover {
private String getValve() {
System.out.println("h");
int i=0;
if(i>0){
System.out.println("1");
}else{
System.out.println("0");
}
return "";
}
public void tets2() {
this.getValve();
}
}
测试类(在test文件夹下)
@Autowired
private TestCodeCover testCodeCover;
@Test
public void tet() {
testCodeCover.tets2();
}
3、进行打包(package或者install都行)
不要跳过测试类
4、生成的文件
image.png
5、进行访问
image.png
6、查看具体的
image.png
网友评论