[JAVA] Introduced jacoco

Install jacoco to get coverage when TestNG is executed.

Coverage acquisition mechanism

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.

Preparation

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 run

Test it in Maven and when you're done, "view the report" in your code coverage.

Recommended Posts

Introduced jacoco
Rails6 jQuery introduced
Rails Bootstrap introduced
Introduced Dry :: Struct
Introduced Rails6 toastr
Introduced docker-ce (CentOS 7.9)