在服务器上执行webApplication时如何获取JaCoCo工具代码覆盖率 [英] How to get JaCoCo instrumentation code coverage while executing webApplication on server

查看:255
本文介绍了在服务器上执行webApplication时如何获取JaCoCo工具代码覆盖率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在实时应用程序上执行某些场景时(即,当应用程序在服务器上运行时),我想获取代码覆盖率报告(或跟踪应用程序的控制器流),因此我引用了官方的

I want to get a code coverage report (or tracking controller flow of an application) when I execute the some scenario on live application i.e. while application running in server, so I have refereed the official Jacoco documentation and did as fallows

第一步,我试图将jacoco代理绑定到运行端口的应用程序

As a first step, I tried to bind the jacoco agent to the application running port

export MAVEN_OPTS=-javaagent:/home/user/.m2/repository/org/jacoco/org.jacoco.agent/0.7.4.201502262128/org.jacoco.agent-0.7.4.201502262128-runtime.jar=includes=*,output=tcpserver,port=6300,address=*,dumponexit=true  

那时候我已经启动了服务器并执行了一些测试方案,那时我还要求jacoco通过执行以下命令来获取转储

then I have started the server and executed some test scenarios by that time I have also requested jacoco to get the dump by executing following command

 mvn jacoco:dump

当我首先执行上述命令时,它显示为java.net.BindException: Address already in use,然后显示为休止符

when I executed the above command first it showed like java.net.BindException: Address already in use then it printed as fallows

[INFO] Connecting to localhost/127.0.0.1:6300

当我要求通过执行以下命令来生成jacoco报告后,当我停止服务器 jacoco.exec 到那里的已知位置时,

When I stopped the server jacoco.exec has been generated to a known location there after I have requested to generate a jacoco report by executing the following command

mvn jacoco:report

最后我得到了报告,但是当我打开index.html页面时,它显示了0%覆盖率.

Finally I got the report but when I open the index.html page it showed 0% coverage.

您可以通过以下示例Spring MVC项目来重现我的难题

you can reproduce my difficulty with following sample Spring MVC project

请从 https://github.com/mkyong/spring4下载Spring MVC项目-来自Github的-mvc-ajax-example

在我的情况下,我正在使用WildFly 8.x服务器运行项目并将以下依赖项添加到下载的项目pom.xml

In my case I am using WildFly 8.x server to run the project and add the following dependencies to downloaded projects pom.xml

 maven-surefire-plugin of 2.19.1 version
 jacoco-maven-plugin of 0.7.4.201502262128 version 
 junit of 4.12 version

然后使用以下命令清理并构建项目.

Then clean and build the project by using following commands.

mvn clean install

完成后,从项目的目标文件夹中获取.war文件,并将其保存在wildfly serverwildfly-9.0.0.CR2/standalone/deployments/文件夹中

once it is done get the .war file from target folder of the project and keep inside the wildfly serverwildfly-9.0.0.CR2/standalone/deployments/ folder

一旦部署了.war,请转到/wildfly-9.0.0.CR2/bin/文件夹并执行/运行./standalone.sh文件,或者根据平台的不同,您可以运行.dat/.sh文件

Once the .war is deployed go to /wildfly-9.0.0.CR2/bin/ folder and execute/run the ./standalone.sh file or depending on platform you can run the.dat/.sh file

一旦部署项目并启动服务器,请执行以下命令

once project is deployed and server is started execute the following command

mvn jacoco:dump

它显示为聆听/已连接到localhost/127.0.0.1:6300

it displayed like listening/connected to localhost/127.0.0.1:6300

然后执行一些测试方案并执行以下maven命令

Then execute some test scenarios and execute following maven command

mvn jaococ:report

最终将以0%的覆盖率生成报告!.

Finally report will be generated with 0% coverage!.

下载的项目可以在中找到MKyong网站

请帮助我获取代码覆盖率报告,我们将不胜感激.

Please help me to get the code coverage report and any help will be appreciated.

推荐答案

JaCoCo需要与执行时使用的生成报告完全相同的类文件,因此

JaCoCo requires the exact same class files for report generation that were used at execution time, so

  • 如果报告完全为空,则未提供类
  • 如果报告包含类,但其覆盖率是0%,则它们与运行时使用的类不匹配-JaCoCo文档第,并且无论哪种情况,都要检查日志中是否存在警告.

    and in either case check existence of warnings in log.

    这是我所做的:

    • 将JaCoCo 0.7.9下载并解压到/tmp/jacoco/jacoco-0.7.9
    • 将Wildfly 9.0.0.CR2下载并解压到/tmp/jacoco/wildfly-9.0.0.CR2
    • 克隆了 https://github.com/mkyong/spring4-mvc-ajax- 放入/tmp/jacoco/spring4-mvc-ajax-example并以mvn verify
    • /tmp/jacoco/spring4-mvc-ajax-example/spring4-mvc-maven-ajax-example-1.0-SNAPSHOT.war复制到/tmp/jacoco/wildfly-9.0.0.CR2/standalone/deployments
    • Wildfly从JAVA_OPTS=-javaagent:/tmp/jacoco/jacoco-0.7.9/lib/jacocoagent.jar=output=tcpserver ./standalone.sh开始,并有足够的时间来部署应用程序
    • 在目录/tmp/jacoco/spring4-mvc-ajax-example中的
    • 执行了mvn org.jacoco:jacoco-maven-plugin:0.7.9:dump org.jacoco:jacoco-maven-plugin:0.7.9:report(请注意,所用代理的版本与jacoco-maven-plugin的版本匹配),以便它创建了/tmp/jacoco/spring4-mvc-ajax-example/jacoco.exec并报告/tmp/jacoco/spring4-mvc-ajax-example/site/jacoco:
    • 打开http://localhost:8080/spring4-mvc-maven-ajax-example-1.0-SNAPSHOT/并进行了一些操作
    • 再次执行
    • mvn org.jacoco:jacoco-maven-plugin:0.7.9:dump org.jacoco:jacoco-maven-plugin:0.7.9:report以获得更新的报告:
    • downloaded and unpacked JaCoCo 0.7.9 into /tmp/jacoco/jacoco-0.7.9
    • downloaded and unpacked Wildfly 9.0.0.CR2 into /tmp/jacoco/wildfly-9.0.0.CR2
    • cloned https://github.com/mkyong/spring4-mvc-ajax-example into /tmp/jacoco/spring4-mvc-ajax-example and built as mvn verify
    • copied /tmp/jacoco/spring4-mvc-ajax-example/spring4-mvc-maven-ajax-example-1.0-SNAPSHOT.war into /tmp/jacoco/wildfly-9.0.0.CR2/standalone/deployments
    • Wildfly started as JAVA_OPTS=-javaagent:/tmp/jacoco/jacoco-0.7.9/lib/jacocoagent.jar=output=tcpserver ./standalone.sh and got enough time to deploy application
    • in directory /tmp/jacoco/spring4-mvc-ajax-example executed mvn org.jacoco:jacoco-maven-plugin:0.7.9:dump org.jacoco:jacoco-maven-plugin:0.7.9:report (note that version of used agent matches version of jacoco-maven-plugin) so that it created /tmp/jacoco/spring4-mvc-ajax-example/jacoco.exec and report /tmp/jacoco/spring4-mvc-ajax-example/site/jacoco:
    • opened http://localhost:8080/spring4-mvc-maven-ajax-example-1.0-SNAPSHOT/ and did some actions
    • executed mvn org.jacoco:jacoco-maven-plugin:0.7.9:dump org.jacoco:jacoco-maven-plugin:0.7.9:report again to get an updated report:

    这篇关于在服务器上执行webApplication时如何获取JaCoCo工具代码覆盖率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆