DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • SonarQube Analysis With Ginkgo on Mac
  • How to Test Gradle Plugins

Trending

  • Streamlining Event Data in Event-Driven Ansible
  • Agentic AI for Automated Application Security and Vulnerability Management
  • How Trustworthy Is Big Data?
  • Stateless vs Stateful Stream Processing With Kafka Streams and Apache Flink
  1. DZone
  2. Software Design and Architecture
  3. Performance
  4. Verifying End-to-End Test Code Coverage Using Jacoco Agent

Verifying End-to-End Test Code Coverage Using Jacoco Agent

End-to-end testing is important to ensure the quality of the product you'll be shipping. See how to implement an end-to-end testing scenario in this walkthrough.

By 
Paweł Szeliga user avatar
Paweł Szeliga
·
Nov. 11, 17 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
55.7K Views

Join the DZone community and get the full member experience.

Join For Free

Fact: end-to-end tests are critical if you want to make sure your software works as it should. To be 100% sure that you covered every (or almost every) possible branch of your business code, it is worth it to check what code has been invoked after your E2E suite finished successfully. The solution I am going to present may be much easier if you are able to stand up your application locally. Sometimes, though, it is not the case.

In this scenario, we are going to start two instances of a web application at the remote servers, with code invocation recording turned on. Next, we are going to run integration tests pointing to them one after another with different parameters, to test different paths. After the tests are done, we are going to pull the data from these servers to localhost, merge it and transform it to HTML report.

We are going to test this simple REST controller:

@RestController
public class ExampleController {

    @GetMapping(path = "/test")
    public int exampleMethod(@RequestParam int parameter) {
        if(parameter > 10) {
            return invokeThisBranch(parameter);
        } else {
            return invokeAnotherBranch(parameter);
        }
    }

    private int  invokeThisBranch(int parameter) {
        System.out.println("Taking this branch..");
        if(parameter < 10) {
            System.out.println("This cannot be tested..");
        }
        return 3;
    }

    private int invokeAnotherBranch(int parameter) {
        System.out.println("Taking another branch..");
        return 3;
    }
}

To make things simple, we will run two instances of the same application on our local machines (on different ports). Before we do that, we need to download Jacoco agent from this link. Remember that the version must match the version of your Jacoco plugin in Maven. This agent will be attached to JVM and record the code coverage. The problem is that you need to push it to your remote server manually (if you find a way to automate this clean way, let me know). If you are using IntelliJ, add this to VM options in your Run Configuration (or add this to startup script on a remote server):

-javaagent:<path-to-agent>/jacocoagent.jar=port=36320,destfile=jacoco-it.exec,output=tcpserver

By specifying the output as a tcpserver and providing a port, you are enabling further connection from the Maven plugin, which will download a data file (jacoco-it.exec).

Our test suite contains one test, which will cause different methods to be invoked depending on the parameter value.

@RunWith(SpringRunner.class)
public class TestCoverageExampleApplicationTests {

    private TestRestTemplate testRestTemplate = new TestRestTemplate();

    @Test
    public void anotherBranchTest() {
        //given
        int appPort =  Integer.parseInt(System.getProperty("app.port"));
        int parameter = Integer.parseInt(System.getProperty("parameter"));

        //when
        Integer result = testRestTemplate.getForObject(
                "http://localhost:" + appPort + "/test?parameter=" + parameter, Integer.class);

        //then
        assertThat(result).isEqualTo(3);
    }
}

We want to make sure that even though the code coverage was spread among instances, we will merge it correctly to be able to see the whole picture. Let’s run the test - different paths for different instance:

mvn test -Dapp.port=8080 -Dparameter=5
mvn test -Dapp.port=8081 -Dparameter=15

Before we generate a coverage report, we need to configure Jacoco plugins in the pom.xml file. The full version can be found on my GitHub account:

<plugins>
    <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>${jacoco.version}</version>
        <executions>
            <execution>
                <id>pull-test-data</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>dump</goal>
                </goals>
                <configuration>
                    <destFile>${project.build.directory}/jacoco-it-${app.host}:${app.port}.exec</destFile>
                    <address>${app.host}</address>
                    <port>${app.port}</port>
                    <reset>false</reset>
                    <skip>${skip.dump}</skip>
                </configuration>
            </execution>
            <execution>
                <id>merge-test-data</id>
                <goals>
                    <goal>merge</goal>
                </goals>
                <configuration>
                    <destFile>target/jacoco-it.exec</destFile>
                    <skip>${skip.dump}</skip>
                    <fileSets>
                        <fileSet implementation="org.apache.maven.shared.model.fileset.FileSet">
                            <directory>target</directory>
                            <includes>
                                <include>*it*.exec</include>
                            </includes>
                        </fileSet>
                    </fileSets>
                </configuration>
            </execution>
        </executions>
        <configuration>
            <append>true</append>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <version>${antrun-plugin.version}</version>
        <executions>
            <execution>
                <id>generate-report</id>
                <phase>post-integration-test</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <skip>${skip.int.tests.report}</skip>
                    <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="${basedir}">
                                    <include name="target/jacoco-it*.exec"/>
                                </fileset>
                            </executiondata>
                            <structure name="jacoco-multi Coverage Project">
                                <group name="jacoco-multi">
                                    <classfiles>
                                        <fileset dir="target/classes"/>
                                    </classfiles>
                                    <sourcefiles encoding="UTF-8">
                                        <fileset dir="src/main/java"/>
                                    </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>${jacoco.ant.version}</version>
            </dependency>
        </dependencies>
    </plugin>
</plugins>

Let’s create an automated script for coverage report generation:

mvn jacoco:dump@pull-test-data -Dapp.host=localhost -Dapp.port=36320 -Dskip.dump=false
mvn jacoco:dump@pull-test-data -Dapp.host=localhost -Dapp.port=36321 -Dskip.dump=false
mvn jacoco:merge@merge-test-data -Dskip.dump=false
mvn antrun:run@generate-report -Dskip.int.tests.report=false

The script will pull the data files from two remote servers (localhost, in our case), merge them into one file, and then generate a report from it.

The final report is a joy to analyze:

Image title

Our class:

Image title

As you can see, even though different code was invoked in different instances, the final report contains both paths.

Some real-world problems you may encounter:

  • If you use bytecode manipulators like Lombok or AspectJ, Jacoco won’t be able to find a source code that matches the invoked lines; you can use auto-value or immutables instead of Lombok for some use cases and spring-aop instead of AspectJ.
  • If you write your tests in Spock and you want to upload your jacoco-it.exec files to Sonar to show the code coverage there, you have to make sure Groovy’s expressive method names will be correctly transformed in the failsafe report - you need to add org.sonar.java.jacoco.JUnitListener as a listener.

The full code can be found on my GitHub account.

Testing Code coverage

Opinions expressed by DZone contributors are their own.

Related

  • Creating a Web Project: Refactoring
  • Enhancing Testing Efficiency: Transitioning From Traditional Code Coverage to Code Change Coverage
  • SonarQube Analysis With Ginkgo on Mac
  • How to Test Gradle Plugins

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!