Client / Server Web Application Code Coverage

I am writing a multi-module application. Some of the modules are simply basic Java libraries, which are then included in the WAR of web applications.

I would like to run code coverage in the following scenario:

  • I run webapp through the built-in Jetty, which runs through Maven.

  • I have tests that execute HTTP requests against webapp.

  • I would like to get the code covered in webapp as well as tests.

Is this possible, and how can this be achieved with Cobertura, JaCoCo or Emma? As I understand it, code coverage will only cover client code in this scenario. Am I right?

+4
source share
4 answers

, JaCoCo-agent jvm, , , webapp. , , .

JaCoCo Maven - , . .

: -, , ,

+2

Jacoco . , junit.

<plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.5.10.201208310627</version>
                <configuration>
                    <skip>${maven.test.skip}</skip>
                    <output>file</output>
                    <append>true</append>
                </configuration>
                <executions>
                    <execution>
                        <id>jacoco-initialize</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>jacoco-site</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>

.. , maven eclipse, , . jacoco , .

+1

, pom.xml:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</
  <version>0.7.4.201502262128</vers
</plugin>
  • JaCoCo jacocoagent.jar (, $HOME/tools/jacocoagent0.7.4.jar)

  • JaCoCo Maven JVM :

    export MAVEN_OPTS="$MAVEN_OPTS \
    -javaagent:$HOME/tools/jacocoagent0.7.4.jar=output=tcpserver,port=6300"
    
  • , . mvn jetty:run

  • mvn jacoco:dump jacoco:report

  • ./target/site/index.html ( )

+1

, . . , , , jvmargs jacoco javaagent. api java- . pom jacoco

<plugin>
    <groupId>org.jacoco</groupId>
    <artifactId>jacoco-maven-plugin</artifactId>
    <version>${jacoco-maven-plugin.version}</version>
    <configuration>
        <append>true</append>
    </configuration>
    <executions>
        <execution>
            <id>prepare-test</id>
            <goals>
                <goal>prepare-agent</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>surefireArgLine</propertyName>
            </configuration>
        </execution>
        <execution>
            <id>prepare-integration</id>
            <goals>
                <goal>prepare-agent-integration</goal>
            </goals>
            <configuration>
                <destFile>${project.build.directory}/jacoco.exec</destFile>
                <propertyName>failsafeArgLine</propertyName>
            </configuration>
        </execution>
    </executions>
</plugin>

exec unit integration.

<plugin>
    <groupId>org.eclipse.jetty</groupId>
    <artifactId>jetty-maven-plugin</artifactId>
    <version>${jetty-maven-plugin.version}</version>
    <configuration>
        <stopKey>foo</stopKey>
        <stopPort>9999</stopPort>
        <webApp>
            <contextPath>/myway</contextPath>
            <descriptor>src/main/webapp/WEB-INF/web.xml</descriptor>
        </webApp>
        <!-- passing the jacoco plugin as a jvmarg -->
        <jvmArgs>${failsafeArgLine}</jvmArgs>
    </configuration>
    <executions>
        <execution>
            <id>start-jetty</id>
            <phase>pre-integration-test</phase>
            <configuration>
                <daemon>true</daemon>
                <waitForChild>false</waitForChild>
            </configuration>
            <goals>
                <goal>run-forked</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-jetty</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

jvm jvmargs. , pom. , , .

<reporting>
  </plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco-maven-plugin.version}</version>
        <reportSets>
            <reportSet>
                <id>jacoco-report</id>
                <reports>
                    <report>report</report>
                </reports>
            </reportSet>
        </reportSets>
      </plugin>
    </plugins>
</reporting>

Access to the reports can be obtained in the target / site / jacoco / index.html file, alternately you can run it from the command line.

mvn jacoco: report

Hope this helps.

+1
source

All Articles