Install jacoco to get coverage when TestNG is executed.
I have only touched jacoco and Cobertura, but both have the following flow. I don't know the details.
Therefore, it would be very disappointing to create an archive based on the class file rewritten by the instrument. Please be careful.
Maven is easy. It is possible to measure on-the-fly instrumentation at runtime, but there were some cases where the coverage was 0% when using PowerMock. Therefore, we have adopted the method (offline instrumentation) of modifying the class file in advance.
I am using NetBeans IDE 8.2 this time, but I dared to install the old version 0.8.1 because the result report was not displayed correctly due to a bug on the IDE side.
pom.xml(dependencies)
<dependency>
<!--Offline Instrument doesn't work without it-->
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.1</version>
<scope>test</scope>
<classifier>runtime</classifier>
</dependency>
pom.xml(build/plugins)
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
<configuration>
<systemPropertyVariables>
<jacoco-agent.destfile>target/jacoco.exec</jacoco-agent.destfile>
</systemPropertyVariables>
<!--↓ Without this, the stack trace will not appear-->
<trimStackTrace>false</trimStackTrace>
</configuration>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<!-- 0.8.2 @NetBeans IDE 8.If it is 2, "code coverage" is not recorded-->
<version>0.8.1</version>
<executions>
<execution>
<id>default-instrument</id>
<goals>
<goal>instrument</goal>
</goals>
</execution>
<execution>
<id>default-restore-instrumented-classes</id>
<goals>
<goal>restore-instrumented-classes</goal>
</goals>
</execution>
<execution>
<id>default-report</id>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
Test it in Maven and when you're done, "view the report" in your code coverage.