How to get JaCoCo instrumentation code coverage while executing webApplication on server

[亡魂溺海] 提交于 2019-12-23 05:06:30

问题


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

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  

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

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

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

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

you can reproduce my difficulty with following sample Spring MVC project

please download Spring MVC project from https://github.com/mkyong/spring4-mvc-ajax-example from the Github

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

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

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

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

Then execute some test scenarios and execute following maven command

mvn jaococ:report

Finally report will be generated with 0% coverage!.

Downloaded project can be found in MKyong website

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


回答1:


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

  • if report is completely empty, then classes were not provided
  • if report contains classes but their coverage is 0%, then they don't match classes that were used at runtime - this is described along with other related information in JaCoCo documentation on page http://www.jacoco.org/jacoco/trunk/doc/classids.html

and in either case check existence of warnings in log.

Update for updated question

Here is what I did:

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


来源:https://stackoverflow.com/questions/42133100/how-to-get-jacoco-instrumentation-code-coverage-while-executing-webapplication-o

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!