客户端/服务器 Web 应用程序的代码覆盖率

2024-05-09Java开发问题
12

本文介绍了客户端/服务器 Web 应用程序的代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在编写一个多模块应用程序.一些模块只是基本的 Java 库,然后包含在 webapp 的 WAR 中.

I am writing a multi-module application. Some of the modules are just basic Java libraries which are then included in the WAR of a webapp.

我想在以下场景中运行代码覆盖:

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

  • 我正在通过一个通过 Maven 启动的嵌入式 Jetty 运行 webapp.

  • I am running the webapp through an embedded Jetty that is started via Maven.

我有针对 webapp 执行 HTTP 请求的测试.

I have tests which are executing HTTP requests against the webapp.

我想获得包含在 webapp 和测试中的代码.

I would like to get code covered in the webapp and also by the tests.

这可能吗?如何使用 Cobertura、JaCoCo 或 Emma 来实现?据我了解,在这种情况下,代码覆盖率只会覆盖客户端代码.我说的对吗?

Is this possible and how can it be achieved with Cobertura, JaCoCo or Emma? From what I understand, the code coverage will only cover the client-side code in this scenario. Am I correct?

推荐答案

我是这样实现的

假设你已经有一个最小的 pom.xml 配置:

Assuming you already have a minimal pom.xml config:

<plugin>
  <groupId>org.jacoco</groupId>
  <artifactId>jacoco-maven-plugin</
  <version>0.7.4.201502262128</vers
</plugin>

  1. 下载JaCoCo的代理并将jacocoagent.jar复制到合适的位置(例如$HOME/tools/jacocoagent0.7.4.jar)

  1. Download JaCoCo's agent and copy jacocoagent.jar to a suitable location (e.g. $HOME/tools/jacocoagent0.7.4.jar)

通过以下方式将 JaCoCo 的代理连接到 Maven 的 JVM:

Attach JaCoCo's agent to Maven's JVM via:

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

  • 使用嵌入式码头服务器运行您的应用程序,例如mvn jetty:run

    运行您的集成测试

    在另一个 shell 中,通过 mvn jacoco:dump jacoco:report

    In another shell, dump and report via mvn jacoco:dump jacoco:report

    ./target/site/index.html 上打开您的报告(默认)

    Open your report on ./target/site/index.html (by default)

    这篇关于客户端/服务器 Web 应用程序的代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

    The End

  • 相关推荐

    如何使用 JAVA 向 COM PORT 发送数据?
    How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
    2024-08-25 Java开发问题
    21

    如何使报表页面方向更改为“rtl"?
    How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
    2024-08-25 Java开发问题
    19

    在 Eclipse 项目中使用西里尔文 .properties 文件
    Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
    2024-08-25 Java开发问题
    18

    有没有办法在 Java 中检测 RTL 语言?
    Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
    2024-08-25 Java开发问题
    11

    如何在 Java 中从 DB 加载资源包消息?
    How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
    2024-08-25 Java开发问题
    13

    如何更改 Java 中的默认语言环境设置以使其保持一致?
    How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
    2024-08-25 Java开发问题
    13