2

I have been trying to get code coverage of actual development code (servlets and others) using integration tests (code coverage is not for integration tests).

Tech stack: Java 1.8, Maven 3.3.9 and IntelliJ Idea 2018.3

My project structure is as below

  • IntegrationTest Module
  • Servlet Module (war)
  • Module 1
  • Module 2

To run my integration tests (IT), I have configured Tomcat in IntelliJ to run on 8081 port. To execute the IT, we keep posting to the local host at http://localhost:8081/rm-xsp/servletname

Below is my pom and mvn clean verify -Pone is my maven goal

<properties>
<!--Servlet code-->
    <server.classes>../rm-xsp-war/target/classes</server.classes>
    <server.sources>../rm-xsp-war/src/main/java</server.sources>

    <!--Servlet dependency code-->
    <mms-main.classes>../rm-xsp-java/target/classes</mms-main.classes>
    <mms-main.sources>../rm-xsp-java/src/main/java</mms-main.sources>
</properties>

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <version>2.7.1</version>
            <configuration>
                <systemPropertyVariables>
                    <jacoco-agent.destfile>${basedir}/target/coverage-reports/jacoco-it.exec</jacoco-agent.destfile>
                </systemPropertyVariables>
                <parallel>methods</parallel>
                <threadCount>5</threadCount>

            </configuration>
            <executions>
                <execution>
                    <id>integration-test</id>
                    <goals>
                        <goal>integration-test</goal>
                    </goals>
                </execution>
                <execution>
                    <id>verify</id>
                    <goals>
                        <goal>verify</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

<plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <version>0.7.5.201505241946</version>
            <executions>
                <!-- prepare agent for measuring integration tests -->
                <execution>
                    <id>prepare-integration-tests</id>
                    <phase>pre-integration-test</phase>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                    <configuration>
                        <destFile>${basedir}/target/coverage-reports/jacoco-it.exec</destFile>
                        <propertyName>failsafeArgLine</propertyName>
                    </configuration>
                </execution>
                <execution>
                    <id>default-instrument</id>
                    <goals>
                        <goal>instrument</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-restore-instrumented-classes</id>
                    <phase>post-integration-test</phase>
                    <goals>
                        <goal>restore-instrumented-classes</goal>
                    </goals>
                </execution>
                <execution>
                    <id>default-report</id>
                    <phase>verify</phase>
                    <goals>
                        <goal>report</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-it.exec</dataFile>
                    </configuration>
                </execution>
                <execution>
                    <id>default-report-integration</id>
                    <goals>
                        <goal>report-integration</goal>
                    </goals>
                    <configuration>
                        <dataFile>${basedir}/target/coverage-reports/jacoco-it.exec</dataFile>
                    </configuration>
                </execution>
            </executions>
        </plugin>


<profiles>
    <profile>
        <id>one</id>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-antrun-plugin</artifactId>
                    <version>1.7</version>
                    <executions>
                        <execution>
                            <id>post-test-report</id>
                            <phase>verify</phase>
                            <goals>
                                <goal>run</goal>
                            </goals>
                            <configuration>
                                <target>
                                    <taskdef name="report" classname="org.jacoco.ant.ReportTask">
                                        <classpath path="${basedir}/target/jacoco-jars/org.jacoco.ant.jar"/>
                                    </taskdef>
                                    <mkdir dir="${basedir}/target/coverage-report"/>
                                    <report>
                                        <executiondata>
                                            <fileset dir="${build.directory}">
                                                <include name="jacoco.exec"/>
                                            </fileset>
                                        </executiondata>
                                        <structure name="Integration Test Coverage Project">
                                            <group name="Server">
                                                <classfiles>
                                                    <fileset dir="${server.classes}"/>
                                                    <fileset dir="${mms-main.classes}"/>
                                                </classfiles>
                                                <sourcefiles encoding="UTF-8">
                                                    <fileset dir="${server.sources}"/>
                                                    <fileset dir="${mms-main.sources}"/>
                                                </sourcefiles>
                                            </group>
                                        </structure>
                                        <html destdir="${basedir}/target/coverage-report/html"/>
                                        <xml destfile="${basedir}/target/coverage-report/coverage-report.xml"/>
                                        <csv destfile="${basedir}/target/coverage-report/coverage-report.csv"/>
                                    </report>
                                </target>
                            </configuration>
                        </execution>
                    </executions>
                    <dependencies>
                        <dependency>
                            <groupId>org.jacoco</groupId>
                            <artifactId>org.jacoco.ant</artifactId>
                            <version>0.8.0</version>
                        </dependency>
                    </dependencies>
                </plugin>
            </plugins>
        </build>
    </profile>
</profiles>

I see that coverage-report folder gets created with html folder under target, containing the report. But none of the java class file has any coverage. Refer below:

Report would look like this..

My output as below:

> [INFO] ---
> jacoco-maven-plugin:0.7.5.201505241946:restore-instrumented-classes
> (default-restore-instrumented-classes) @ autoTestEnv --- 
> [INFO] 
> [INFO] --- maven-failsafe-plugin:2.7.1:verify (verify) @ autoTestEnv
> --- [INFO] Failsafe report directory: C:\Users\workspace\autoTestEnv\target\failsafe-reports
>  [INFO]
> --- jacoco-maven-plugin:0.7.5.201505241946:report (default-report) @ autoTestEnv --- [INFO] Skipping JaCoCo execution due to missing
> execution data
> file:C:\Users\workspace\autoTestEnv\target\coverage-reports\jacoco-it.exec
> [INFO]  [INFO] ---
> jacoco-maven-plugin:0.7.5.201505241946:report-integration
> (default-report-integration) @ autoTestEnv --- [INFO] Skipping JaCoCo
> execution due to missing execution data
> file:C:\Users\workspace\autoTestEnv\target\coverage-reports\jacoco-it.exec
> [INFO]  [INFO] --- maven-antrun-plugin:1.7:run (post-test-report) @
> autoTestEnv --- [INFO] Executing tasks
> 
> main:
>     [mkdir] Created dir: C:\Users\workspace\autoTestEnv\target\coverage-report    [report]
> Writing bundle 'Server' with 155 classes

Seems I have missing something. I have gone through a lot of posts on the same but couln't figure out the solution.

Any pointers or suggestions are highly appreciated.

Sham
  • 830
  • 9
  • 27
  • The `instrument` goal does not seem to be bound to any phase (do you see it running in the maven logs?). Try binding it to the `pre-integration-test` – guido Mar 16 '19 at 16:43
  • @guido Tried, but no luck. I still see code coverage as '0'. My IT ran successfully. Kindly refer my test execution output in the above post. – Sham Mar 18 '19 at 06:05
  • It seems you are defining: `failsafeArgLine`, but then you are not setting it for failsafe execution; try adding it: `${failsafeArgLine}` in the `` element of maven-failsafe-plugin – guido Mar 18 '19 at 09:47
  • Tried it. I see that `jacoco-it.exec` is created now. But when the reporting comes in, I see `[mkdir] Created dir: C:\Users\WorkSpace\usage_service\autoTestEnv\target\coverage-report [report] Loading execution data file C:\Users\WorkSpace\usage_service\autoTestEnv\target\coverage-reports\jacoco-it.exec` message. I feel that the reporting is looking at target\classes of autoTestEnv folder and not servlet folder. – Sham Mar 18 '19 at 11:56
  • The only reason for which you find the file at that location, is that you setting it in the configuration; when jacoco instruments the classes, it uses the classes that were collected in target – guido Mar 18 '19 at 15:46
  • That's correct. However, I was not able to figure out the issue and it remains the same :( Thanks @guido for inputs. – Sham Mar 19 '19 at 16:50

0 Answers0