Generate Code Coverage with JaCoCo and spring-boot-maven-plugin

Simon Martinelli

During integration tests I use spring-boot-maven-plugin to start my spring application:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
         <execution>
             <id>start-spring-boot</id>
             <phase>pre-integration-test</phase>
             <goals>
                 <goal>start</goal>
             </goals>
         </execution>
         <execution>
             <id>stop-spring-boot</id>
             <phase>post-integration-test</phase>
             <goals>
                 <goal>stop</goal>
             </goals>
         </execution>
     </executions>
</plugin>

Now I would like to add JaCoCo agent to the exection but adding it to the configuration as agent or jvmarguments didn't work. When starting I always see:

[INFO] --- spring-boot-maven-plugin:2.2.4.RELEASE:start (start-spring-boot) @ tosca-ui ---
[INFO] Attaching agents: []

How can I use JaCoCo with spring-boot-maven-plugin?

Maciej Walkowiak

By default Spring Boot Maven plugin creates a fork and the agent configuration has to be explicitly specified. It can be done through setting <agents><agent>...</agent></agents> configuration property in Spring Boot Maven plugin BUT it does not work with Jacoco since there is no idiomatic way to find out what's the exact path to the agent jar file.

Instead you can pass argLine variable set by Jacoco to Spring Boot Maven plugin JVM arguments:

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <configuration>
        <jvmArguments>${argLine}</jvmArguments>
    </configuration>
    <executions>
        <execution>
            <id>start-spring-boot</id>
            <phase>pre-integration-test</phase>
            <goals>
                <goal>start</goal>
            </goals>
        </execution>
        <execution>
            <id>stop-spring-boot</id>
            <phase>post-integration-test</phase>
            <goals>
                <goal>stop</goal>
            </goals>
        </execution>
    </executions>
</plugin>

Since quite a few Maven plugins have to be configured to make it work, just to avoid any confusion here's the complete pom.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>jacoco-spring-boot-maven</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>jacoco-spring-boot-maven</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>11</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <jvmArguments>${argLine}</jvmArguments>
                </configuration>
                <executions>
                    <execution>
                        <id>start-spring-boot</id>
                        <phase>pre-integration-test</phase>
                        <goals>
                            <goal>start</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>stop-spring-boot</id>
                        <phase>post-integration-test</phase>
                        <goals>
                            <goal>stop</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>3.0.0-M4</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.jacoco</groupId>
                <artifactId>jacoco-maven-plugin</artifactId>
                <version>0.8.5</version>
                <executions>
                    <execution>
                        <id>default-prepare-agent</id>
                        <goals>
                            <goal>prepare-agent</goal>
                        </goals>
                    </execution>
                    <execution>
                        <id>default-report</id>
                        <phase>verify</phase>
                        <goals>
                            <goal>report</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>
</project>

With this, once you execute mvn clean verify you can find Jacoco reports in target/site/jacoco/index.html.

You can find complete sample project at jacoco-spring-boot-maven-plugin-sample

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

maven jacoco plugin does not generate coverage report

From Dev

maven-surefire-plugin, jacoco-maven-plugin show no coverage

From Dev

maven jacoco: not generating code coverage report

From Dev

Sonar maven jacoco code coverage for Multimodule project

From Dev

Spring Boot - Host JaCoCo coverage as static HTML

From Dev

How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

From Dev

How to generate executable scripts using spring boot maven plugin

From Dev

Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1

From Dev

Spring Boot Maven Plugin

From Dev

Spring Boot Maven Plugin

From Dev

Jacoco 0% Code Coverage

From Dev

java code coverage with jacoco

From Dev

Jacoco 0% Code Coverage

From Dev

ProGuard + Spring Boot + Maven Plugin

From Dev

build.gradle with Jacoco plugin doesn't generate coverage report for integration tests

From Dev

Jacoco coverage of unit test code

From Dev

Jacoco Code Coverage in android studio

From Dev

Code coverage with jacoco for a Android library

From Dev

Jacoco code coverage is affected by AspectJ

From Dev

Missing Jacoco Code Coverage and IncompatibleClassChangeError

From Dev

Generate test coverage with Jacoco and Circle CI fails

From Dev

How to generate jacoco coverage in manual tests?

From Dev

How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin?

From Dev

Code coverage in maven build - Skipping JaCoCo execution due to missing classes directory

From Dev

How to check minimum code coverage for multi-module maven project with jacoco?

From Dev

Code coverage in maven build - Skipping JaCoCo execution due to missing classes directory

From Dev

Jacoco Maven multi module project coverage

From Dev

Spring boot and maven exec plugin issue

From Dev

spring boot maven plugin stop goal

Related Related

  1. 1

    maven jacoco plugin does not generate coverage report

  2. 2

    maven-surefire-plugin, jacoco-maven-plugin show no coverage

  3. 3

    maven jacoco: not generating code coverage report

  4. 4

    Sonar maven jacoco code coverage for Multimodule project

  5. 5

    Spring Boot - Host JaCoCo coverage as static HTML

  6. 6

    How can I exclude files of the Code Coverage of SonarQube using JaCoCo maven plugin

  7. 7

    How to generate executable scripts using spring boot maven plugin

  8. 8

    Jacoco and Unit Tests Code Coverage with android-gradle-plugin >= 1.1

  9. 9

    Spring Boot Maven Plugin

  10. 10

    Spring Boot Maven Plugin

  11. 11

    Jacoco 0% Code Coverage

  12. 12

    java code coverage with jacoco

  13. 13

    Jacoco 0% Code Coverage

  14. 14

    ProGuard + Spring Boot + Maven Plugin

  15. 15

    build.gradle with Jacoco plugin doesn't generate coverage report for integration tests

  16. 16

    Jacoco coverage of unit test code

  17. 17

    Jacoco Code Coverage in android studio

  18. 18

    Code coverage with jacoco for a Android library

  19. 19

    Jacoco code coverage is affected by AspectJ

  20. 20

    Missing Jacoco Code Coverage and IncompatibleClassChangeError

  21. 21

    Generate test coverage with Jacoco and Circle CI fails

  22. 22

    How to generate jacoco coverage in manual tests?

  23. 23

    How to report Jacoco Groovy code coverage to Sonar using new Gradle SonarQube plugin?

  24. 24

    Code coverage in maven build - Skipping JaCoCo execution due to missing classes directory

  25. 25

    How to check minimum code coverage for multi-module maven project with jacoco?

  26. 26

    Code coverage in maven build - Skipping JaCoCo execution due to missing classes directory

  27. 27

    Jacoco Maven multi module project coverage

  28. 28

    Spring boot and maven exec plugin issue

  29. 29

    spring boot maven plugin stop goal

HotTag

Archive