5

I checked Jacoco github ans browsed a few Stack Overflow questions . Turns out upto version 0.7.9 of jacoco filtering methods by annotations is not supported , only whole class is supported . As now 0.8.0 and 0.8.1 are released . Is this feature added in those versions ? I checked the change history of jacoco.

https://github.com/jacoco/jacoco/releases

But do not sees anything related to filtering in the latest versions. But still want to confirm if somebody has achieved this and how ?

Pardeep Kumar
  • 900
  • 1
  • 14
  • 30
  • Possible duplicate of [How would I add an annotation to exclude a method from a jacoco code coverage report?](https://stackoverflow.com/questions/47824761/how-would-i-add-an-annotation-to-exclude-a-method-from-a-jacoco-code-coverage-re) – Godin May 21 '18 at 10:31
  • i have the same question but i want to know that now since 0.8.0 and 0.8.1 are released , so do those versions contain such filtering . As i tried i could not find anything like that in both new versions .. but just want to confirm . – Pardeep Kumar May 22 '18 at 08:50
  • Mentioned question has answer with a link to changelog - https://www.jacoco.org/jacoco/trunk/doc/changes.html and there is no such feature in changelog up to 0.8.1, so the same answer still holds. – Godin May 22 '18 at 21:39
  • @Godin Thanks for confirming – Pardeep Kumar May 23 '18 at 04:17

1 Answers1

-1

I found a solution how to exclude static methods from coverage report.

  1. Wrap it with static class
  2. Exclude static class in configuration

Example java code:

private static class Document {
    private static org.w3c.dom.Document createDocument() {
        try {
            final javax.xml.parsers.DocumentBuilderFactory factory =
                    javax.xml.parsers.DocumentBuilderFactory.newInstance();
            factory.setNamespaceAware(true);
            factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, true);
            final javax.xml.parsers.DocumentBuilder builder = factory.newDocumentBuilder();
            return builder.newDocument();
        } catch (javax.xml.parsers.ParserConfigurationException ex) {
            return null;
        }
    }
}

Example exclude configuration:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</artifactId>
  <version>0.8.2</version>
  <executions>
    <execution>
      <id>prepare-agent</id>
      <goals>
        <goal>prepare-agent</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <excludes>
      <exclude>**/Xml$Document.class</exclude>
    </excludes>
  </configuration>
</plugin>
Valentyn Kolesnikov
  • 2,029
  • 1
  • 24
  • 31