使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率

2023-12-28

在集成测试期间,我使用 spring-boot-maven-plugin 来启动我的 spring 应用程序:

<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>

现在我想将 JaCoCo 代理添加到执行中,但将其作为代理或 jvmarguments 添加到配置中不起作用。开始时我总是看到:

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

如何将 JaCoCo 与 spring-boot-maven-plugin 一起使用?


默认情况下,Spring Boot Maven 插件会创建一个分支,并且必须显式指定代理配置。可以通过设置来完成<agents><agent>...</agent></agents>Spring Boot Maven 插件中的配置属性,但它不适用于 Jacoco,因为没有惯用的方法来找出代理 jar 文件的确切路径。

相反,你可以通过argLineJacoco 设置为 Spring Boot Maven 插件 JVM 参数的变量:

<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>

由于必须配置相当多的 Maven 插件才能使其工作,为了避免任何混淆,这里是完整的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>

有了这个,一旦你执行mvn clean verify您可以在以下位置找到 Jacoco 报告:target/site/jacoco/index.html.

您可以在以下位置找到完整的示例项目jacoco-spring-boot-maven-插件-样本 https://github.com/maciejwalkowiak/jacoco-spring-boot-maven-plugin-sample.git

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 JaCoCo 和 spring-boot-maven-plugin 生成代码覆盖率 的相关文章

  • SWIG 类型映射 uint8_t* 从 C/C++ 到 java.nio.ByteBuffer

    我正在尝试将输入和输出缓冲区从 C 传递给 java 类 出于效率原因 我需要使用 ByteBuffer 这两个缓冲区都是在 C 部分中分配的 我需要将它们传递给一个 java 函数 该函数将使用输入缓冲区进行一些计算并将结果写入输出缓冲区
  • 使用 Java-Large 文件查询 JSON 文件

    我正在尝试使用 java 解析下面的 JSON 文件 我需要能够 按 ID 或名称或对象中的任何字段搜索文件 也在字段中搜索空值 搜索应返回整个对象 该文件将会很大 并且搜索应该仍然很省时 id 1 name Mark Robb last
  • AES BadPaddingException

    如果我使用错误的密钥或错误的盐进行解密 则会引发 BadPaddingException 我希望返回一个不正确的字符串 doFinal 导致解密方法出现异常 信息 This is just an example Unfug S F V s
  • 使用用户名进行 Java LDAP 身份验证

    好吧 这让我发疯 我正在尝试使用 Java 创建 LDAP 身份验证 如果我在 SECURITY PRINCIPAL 中使用我的名字和姓氏 一切都很好 这是我的代码 try Hashtable
  • Java 中的 TreeSet 与 C#.net 的等效项

    我有 Java 代码 其中包含TreeSet 我想将代码转换为 C 我可以使用哪个等效集合 如果没有 请提出替代方案 那将是系统 集合 通用 SortedSet
  • 使用 Thymeleaf 时我们应该删除 HTML 属性吗?

    我正在研究 Thymeleaf 发现几乎所有示例中都有 Thymeleaf 的标签值以及标准 HTML 值 例如 这些
  • Knuth-Morris-Pratt 算法

    解决方案是Knuth Morris Pratt 算法 https en wikipedia org wiki Knuth E2 80 93Morris E2 80 93Pratt algorithm 干草堆 AAAAAAAAA 针 AAA
  • Android Studio 找不到 com.android.support:support-v4:19.1.0

    我已将一个项目导入Android Studio但我收到错误 找不到 com android support support v4 19 1 0 我在哪里可以找到这个文件 我已经使用导入了项目Gradle 我有Android Studio v
  • 大型 XML 的 XML 节点到字符串转换

    到目前为止我一直在使用DOM源在我的 Android 应用程序中将 XML 文件转换为字符串 这是我的代码 public String convertElementToString Node element throws Transform
  • Java中的运算符重载和覆盖

    运算符重载和运算符重写有什么区别 它们在继承和控制台程序中是否相同 Java 不支持运算符重载和重写 检查以下引用自的描述 http java sun com docs white langenv Simple doc2 html http
  • 添加和完成 PHP 源代码文档的工具 [关闭]

    Closed 此问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我有几个已完成的较旧的 PHP 项目 其中有很多内容 我想以 javadoc phpDocumentor
  • 更改 Spring Web 应用程序的默认会话超时

    我必须测试一个由 spring 和 jsp 编写的 Web 应用程序 应用程序的默认会话超时为 30 分钟 我想减少会话超时 为此 我改变了web xml文件输入tomcatInstallationLocation conf 但这不起作用
  • 在 Java/GWT 中解析用户时间输入

    解析用户在 GWT 中的文本字段中键入的时间的最佳方法是什么 默认时间格式要求用户完全按照区域设置指定的时间格式输入时间 我想要更加灵活 因为用户可以通过多种不同的方式输入时间 例如 8 8p 8pm 8 15pm 13 15 1315 1
  • C3P0:生产中未返回的连接超时?

    参数unreturnedConnectionTimeout给定时间段后未返回的连接超时 我正在尝试决定是否应该在我的制作中使用它persistence xml 使用它的一大优点是连接池将能够从泄漏的连接中恢复 一个很大的缺点是泄漏的连接将很
  • 为什么我的 Java 路径中添加了“L”?

    我在我的类路径中加载了一个 jar 在 iReport 中 如果重要的话 我确信它具有所需的方法 但是当我尝试测试连接 从而调用该 jar 时 我得到一个 java lang NoSuchMethodError 说它正在引用班上 Lorg
  • 用什么? MVC、MVP 或 MVVM 还是……?

    我将启动一个 Java 项目来开发桌面应用程序 使用什么作为表示层模式 MVC MVP MVVM 或 如果可能的话 举一些可行的小例子 Actually the ultimate post you re looking for is thi
  • 需要同步仅增量计数器吗?

    我使用整数作为计数器 该整数只会增加 并且肯定有多个线程会同时增加它 当没有其他线程尝试访问其值时 在程序执行结束时读取该计数器的值 我假设我不必为这种仅增量计数器使用锁或任何类型的同步 这是正确的吗 如果这有什么区别的话 我用 Java
  • 如何获取 res.drawable 文件夹的路径来复制文件?

    我正在编写我的应用程序AndroidStudio 我的里面有gif文件drawable gifs文件夹 我希望将该文件复制到MediaStore Images Media单击按钮后的文件夹 目前 即使使用发布的一些答案 我也无法获取我的 g
  • selenium 没有找到合适的方法,直到(ExpectedCondition)

    这是有线的问题 我导入的项目运行 100 几个月前 今天我已将其与依赖项一起导入 但存在问题WebDriverWait 这是我的代码 WebDriverWait driverWait new WebDriverWait driver 100
  • 是什么让热部署成为“难题”?

    在工作中 我们经常遇到这样的问题 永久代内存不足 http www jroller com agileanswers entry preventing java s java lang例外 团队负责人认为这是 JVM 中的一个错误 与代码的

随机推荐