Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Jacoco显示错误的覆盖率检查结果_Java_Unit Testing_Jacoco Maven Plugin_Test Coverage - Fatal编程技术网

Java Jacoco显示错误的覆盖率检查结果

Java Jacoco显示错误的覆盖率检查结果,java,unit-testing,jacoco-maven-plugin,test-coverage,Java,Unit Testing,Jacoco Maven Plugin,Test Coverage,我已经通过maven在我的项目中配置了我的Jacoco插件 这是我的jacoco配置 <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.3</version> <configuration> <excludes

我已经通过maven在我的项目中配置了我的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>
          <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>

org.jacoco

我已经执行了测试,显示了一个抽象类94%的覆盖率,我使用它的具体实现测试了这个抽象类

当我运行maven build时

我有以下错误

违反了类的规则 my.package.AbstractParser.1: 线路覆盖率为0.00,但预期最小值为0.80

我试图在测试中使用一个虚拟实现来测试抽象类,但我还是遇到了同样的错误

谁能告诉我我做错了什么吗

编辑: 我找到了失败的原因

我已经编写了一个内联映射初始化

return new HashMap<String, String>() {
    {
      put(input, "");
    }
  };
返回新的HashMap(){
{
输入“”;
}
};
这部分的覆盖率为0%。所以我的测试不包括这部分

但是我累了

 final Map<String, String> defaultMap = new HashMap<>();
  defaultMap.put(input, "");
  return defaultMap;
final Map defaultMap=new HashMap();
defaultMap.put(输入“”);
返回默认地图;

构建过程甚至没有覆盖新代码。有人能解释一下内联初始化为什么会发生这种情况吗?

尝试将其添加到gradle构建中

android {
    testOptions {
        unitTests {
            all {
                jvmArgs '-noverify'
            }
        }
    }
}
测试和覆盖率存在问题,因此您需要为覆盖率检查配置
jvmArgs
设置,它可以在IDE本身中启用,但当在CI/maven/中运行覆盖率时,应该在gradle中配置它

<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>
          <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%
这意味着每个类别的行覆盖率应至少为80%

return new HashMap<String, String>() {
    {
      put(input, "");
    }
  };
返回新的HashMap(){
{
输入“”;
}
};
声明,顺便说一句,在JaCoCo报告中可以看到什么-请参见下面屏幕截图上的第一行表格

鉴于

  final Map<String, String> defaultMap = new HashMap<>();
  defaultMap.put(input, "");
  return defaultMap;
final Map defaultMap=new HashMap();
defaultMap.put(输入“”);
返回默认地图;
不声明任何类