How to get JaCoCo instrumentation code coverage while executing webApplication on server - 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
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.

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:

Related

Generate dotcover coverage report with jenkins

I have ci server setup on jenkins which builds the .net core 2.2 application. and then generates Test Coverage report using dotcover.
I am executing following command from jenkins job to generate coverage report using dotcover in the directory where Test project's .csproj file exist.
dotnet restore
dotnet dotcover test --dcReportType=HTML --dcOutPut="%WORKSPACE%\TestReport\index.html"
It gives me following error.
[JetBrains dotCover] Failed to perform coverage analysis. Operation
was canceled by the user.
Failed to register x86 core COM object: RegSvr32 failed to register
the core. Exit code is 00000003. Probably you haven't rebooted after a
Windows update. Please reboot the computer and try again.
I have already updated and rebooted as suggested in error but it doesn't solves the issue.
If I run this command directly from Command Line it works fine and generates report properly with the same commands.
Don't know what I am missing in jenkins.

Jacoco integration test of API code coverage

Does Jacoco provide code coverage for integration tests of APIs? That is, I have an instance of my application running locally and I have integration tests where I hit an api offered by my running application instance. In this scenario can I use Jacoco to get how many lines of my running application instance was covered when integration tests where ran?
I have already tried Jacoco's maven plugin's prepare-agent-integration and report-integration goals. But they give code coverage as 0. I think its because jacoco only measures code coverage of the currently ran instance and not the instance whose api is hit.
I had forgot to run the javaagent while running the service. Running the jar file with javaagent with output=tcpserver and then dumping the execution file using Jacoco:dump and creating report using Jacoco:report solved the issue.
java -javaagent:<path_to_agent>/org.jacoco.agent-0.7.9-runtime.jar=output=tcpserver,address=127.0.0.1 -jar myapp.jar
mvn clean verify -Pintegration-tests
mvn jacoco:report -DdataFile=./target/jacoco.exec
mvn jacoco:dump -Djacoco.address=localhost -Djacoco.destFile=./service/target/jacoco.exec

How can i configure team city to execute the intern's test runner with phantom js as the configured browser

I'm trying to add a team city build step for my intern unit tests that execute against a web browser, but I'm uncertain how to configure and run phantom js. I have installed this plugin in teamcity which gives me a phantom js build runner:
https://github.com/jonnyzzz/TeamCity.Node
Currently I have two build steps in team city for this. The first step is using the npm build runner with install and update commands.
The second step is using the command line task runner to execute 'intern-runner config=tests/intern'.
Those two steps seem correct to me, but I believe this is going to fail because phantomjs is not yet running.
Would phantomjs be started using a build step? Or do I need to configure the build agent to be running phantomjs already (and if so how would i go about that)?

Unable to execute unit tests in a maven project from within intellij

I have inherited a legacy project with 100s of tests, and dependencies defined within the pom.
All of the tests run when I execute a mvn clean install from the command line, but when I try to execute one of these tests in debug mode from within Intellij i get the following error.
java.lang.NoClassDefFoundError: Could not initialize class
How can I get intellij to recognise these dependencies when trying to run a test in debug mode from the ide?
I managed to solve this by simply changing the working directory location in the Run configuration to point to the correct classpath location. The default location had been taken from a parent project.
It would be very convenient if one was able to debug tests from within IntelliJ. I have run into similar issues with my massive project and have found a workaround for it.
When wishing to debug a test, I have often found it useful to use a remote debugging session like so:
mvn -Dmaven.surefire.debug="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000 -Xnoagent -Djava.compiler=NONE" -Dtest=com.autofunk.TheFunkImplTest test -DfailIfNoTests=false
You can then attach to the above using a remote debug session from within IntelliJ on port 8000.
Therefore, when debugging tests I first check to see whether a simple debug from within Intellij works. If not, I run the above and then attach to it using a debug configuration like so:
IntelliJ execute unit tests without mvn command.
a IntelliJ plugin: MvnRunner
You can run tests using the maven project window in IntelliJ
View - Tool Windows - Maven Projects
Then under the project or module you wish to test open the lifecycle goals and click test.
This will run the currently configured test goal. Now the report is logged into the target directory
I use
https://github.com/destin/maven-test-support-plugin
to view the test results.
You can access this screen once the plugin is installed from the Project window again right click on the projects root and select "Show Test Results" (should be below the maven icon)
Good luck

Jetty runner is not executing the war file

I have a project named as test using spring,hibernate and struts.It is running when am using eclipse.But when i export this project as war and execute it using jetty runner in command prompt it just extracted the project and listed the contents in the browser.But i want the project to be executed.I have tried with some other simple web application war using the same procedure and it works fine.But in my project its not working with jetty.
here is my code for jetty
D:\>java -jar jetty-runner-7.0.0.v20091005.jar test.war
i just listing some console output of jetty
INFO::RUNNER
NO tx manager found
deploying file:D:/test.war #/[webAppContext#86f241#86f241/,null,file:D:/test.war
can any one tell me a solution for my problem
1) use a newer version of runner, 7.6.3.v20120416
2) what context are you trying to navigate to, you might want to experiment with some of the other cli options on the runner to set the context and whatnot from the command line and experiment with that. This site has some good information on using this artifact as well.