简体   繁体   中英

Jacoco Showing Wrong Coverage check Result

I have configure My Jacoco plugin In my project via maven.

Here is my jacoco configuration

<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>

截图

I have executed the Test, and shows 94% coverage for an Abstract Class , I tested this abstract class using it's concrete Implementation.

When i run by maven build

I'm getting following error

Rule violated for class my.package.AbstractParser.1: lines covered ratio is 0.00, but expected minimum is 0.80

I tried to test abstract class using a dummy implementation on Test still I'm getting the same error

Can some one tell me what I'm doing wrong here.

EDIT: I found out the cause for failure

I have written an inline map initialization

return new HashMap<String, String>() {
    {
      put(input, "");
    }
  };

And the coverage was showing 0% against this part . so my test was not covering this part.

But I tired

 final Map<String, String> defaultMap = new HashMap<>();
  defaultMap.put(input, "");
  return defaultMap;

The build pass without even coverage around new code. can some one explain me why it happened with inline initialization ???

Your 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>

means that line coverage should be at least 80% for each class .

return new HashMap<String, String>() {
    {
      put(input, "");
    }
  };

declares anonymous class , what is BTW visible in JaCoCo report - see first table row on screenshot below

报告

Whereas

  final Map<String, String> defaultMap = new HashMap<>();
  defaultMap.put(input, "");
  return defaultMap;

doesn't declare any class.

Try adding this to your gradle build

android {
    testOptions {
        unitTests {
            all {
                jvmArgs '-noverify'
            }
        }
    }
}

There is an issue with tests and coverage, so you need to configure jvmArgs setting for coverage check, it could be enabled in IDE itself, but when running coverage in CI/maven/wherever it should be configured in gradle

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM