2

I have a project that fires up tomcat using the cargo plugin and then runs integration tests against that tomcat instance. My goal is get integration test coverage reports of the code that runs in tomcat (not coverage of my integration tests).

The question is, how do I get code coverage results of code running in tomcat (separate JVM)?

I have been able to get coverage reports of the integration tests itself and java classes within the test module, however these are rather worthless.

In the jacoco-sessions.html file, I can only see classes that are available in the test module's classpath. Classes that are running in the tomcat server are not present.

spy
  • 3,199
  • 1
  • 18
  • 26

2 Answers2

1

In addition to the answer of Godin you also have to make sure that the coverage data of the test classes itself doesn't use the same output file as the coverage data of the code running in the cargo container. I think in my case the coverage-of-the-tests was overwriting the coverage-of-the-code-under-test.

Note that I am using jacoco.exec for the coverage data of the code-under-integration-test. Normally this is used for the unit tests, but there are no unit tests in my module. This way I don't need to configure SonarQube for an extra filename, but if you like you can use another filename here.

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>prepare-agent-integration-cargo</id>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>argLineCargo</propertyName>
            </configuration>
        </execution>
    </executions>
</plugin>

<plugin>
    <groupId>org.codehaus.cargo</groupId>
    <artifactId>cargo-maven2-plugin</artifactId>
    <configuration>
        <container>
            <!-- ... -->
        </container>
        <configuration>
            <properties>
                <cargo.start.jvmargs>${argLineCargo}</cargo.start.jvmargs>
                <!-- ... -->
            </properties>
        </configuration>
        <!-- ... -->
    </configuration>
    <!-- ... -->
</plugin>

And the JaCoCo configuration in the parent POM:

<build>
    <plugins>
        <plugin>
            <groupId>org.jacoco</groupId>
            <artifactId>jacoco-maven-plugin</artifactId>
            <configuration>
                <includes>
                    <include>my/project/package/**/*</include>
                </includes>
            </configuration>
            <executions>
                <execution>
                    <id>prepare-agent</id>
                    <goals>
                        <goal>prepare-agent</goal>
                    </goals>
                </execution>
                <execution>
                    <id>prepare-agent-integration</id>
                    <goals>
                        <goal>prepare-agent-integration</goal>
                    </goals>
                </execution>
                <execution>
                    <id>reports</id>
                    <goals>
                        <goal>report</goal>
                        <goal>report-integration</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <configuration>
                <argLine>@{argLine}</argLine>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-failsafe-plugin</artifactId>
            <configuration>
                <argLine>@{argLine}</argLine>
            </configuration>
        </plugin>
    </plugins>
</build>
murf
  • 364
  • 4
  • 17
0

According to documentation of jacoco-maven-plugin http://www.jacoco.org/jacoco/trunk/doc/prepare-agent-mojo.html :

Prepares a property pointing to the JaCoCo runtime agent that can be passed as a VM argument to the application under test. Depending on the project packaging type by default a property with the following name is set:

  • tycho.testArgLine for packaging type eclipse-test-plugin and
  • argLine otherwise.

argLine property affects maven-surefire-plugin and maven-failsafe-plugin that start JVM with tests - see http://maven.apache.org/surefire/maven-surefire-plugin/test-mojo.html#argLine and http://maven.apache.org/surefire/maven-failsafe-plugin/integration-test-mojo.html#argLine respectively, but this property has no effect on cargo-maven2-plugin that starts JVM with Tomcat.

According to https://stackoverflow.com/a/38435778/244993 and https://codehaus-cargo.github.io/cargo/Tomcat+9.x.html you need to pass property set by jacoco-maven-plugin to cargo-maven2-plugin as cargo.jvmargs.

Godin
  • 9,801
  • 2
  • 39
  • 76