我已经通过 maven 在我的项目中配置了 My Jacoco 插件。
这是我的 jacoco 配置
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.3</version>
<configuration>
<excludes>
</exclude>**/some/package/SomeClass*</exclude>
</excludes>
</configuration>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
<execution>
<id>jacoco-check</id>
<goals>
<goal>check</goal>
</goals>
<configuration>
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>80%</minimum>
</limit>
</limits>
</rule>
</rules>
</configuration>
</execution>
</executions>
</plugin>
我已经执行了测试,并显示抽象类的覆盖率为 94%,我使用它的具体实现测试了这个抽象类。
当我通过 maven build 运行
我收到以下错误
Rule violated for class my.package.AbstractParser.1: lines covered ratio is 0.00, but expected minimum is 0.80
我尝试在测试中使用虚拟实现来测试抽象类,但仍然遇到相同的错误
有人可以告诉我我在这里做错了什么吗?
编辑: 我找到了失败的原因
我写了一个内联 map 初始化
return new HashMap<String, String>() {
{
put(input, "");
}
};
这部分的覆盖率显示为 0%。所以我的测试没有涵盖这部分。
但是我累了
final Map<String, String> defaultMap = new HashMap<>();
defaultMap.put(input, "");
return defaultMap;
构建过程甚至没有覆盖新代码。有人可以解释一下为什么内联初始化会发生这种情况吗???
最佳答案
您的配置
<rules>
<rule>
<element>CLASS</element>
<excludes>
<exclude>*Test</exclude>
</excludes>
<limits>
<limit>
<counter>LINE</counter>
<value>COVEREDRATIO</value>
<minimum>80%</minimum>
</limit>
</limits>
</rule>
</rules>
表示每个类别的线路覆盖率至少应为 80%。
return new HashMap<String, String>() {
{
put(input, "");
}
};
声明anonymous class ,顺便说一句,JaCoCo 报告中可见什么 - 请参阅下面屏幕截图中的第一个表行
鉴于
final Map<String, String> defaultMap = new HashMap<>();
defaultMap.put(input, "");
return defaultMap;
不声明任何类。
关于java - Jacoco 显示错误的覆盖范围检查结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55850382/