Search code examples
javaunit-testingjacoco-maven-plugintest-coverage

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>

Screenshot

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


Solution

  • 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

    report

    Whereas

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

    doesn't declare any class.