This isn't a bug, but a question. Please remove if this isn't the place.
Background
I am running a Dropwizard app (using java -jar myjar.jar server config.yml) inside a docker container via this plugin, and I'm running integration tests inside it. In my integration test, I know for sure that code is being exercised, but Jacoco coverage reports for the integration tests aren't showing coverage.
My Questions
How do I know that Jacoco is properly attached to the code running inside the container? Do I have to do anything special to extract the coverage report from the running container? I would think that a simple shared volume would work, as I am using now, but that does not seem to be the case. I have a hunch that I need to add the jacoco agent parameters to the java -jar that's taking place inside my container, but I'm not sure.
For jacocco to instrument classes on the fly while they are excercises you need to attach the jacoco agent to you startup command. When you do this for unit tests this happens automatically by the jacoco-maven-plugin. To get then the coverage data outside of the container, a host shared volume is probably the best solution.
Maybe you add a specific Maven profile with this special configuration and use this for your integration teststs ? Alternatively you could prepare a startup script which depending on an env var adds the jacocco agent or not (and then set the env from the outside).
@rhuss
Thanks for the tip - that worked splendidly!
Here's what I ended up doing:
-P integration-tests-javaagent:./target/dependency/org.jacoco.agent-0.7.9-runtime.jar=destfile=./target/coverage-reports/jacoco-it.exec,append=true
Thanks again!
To anyone who comes across this issue via Googling for answers: If you are running your Dropwizard integration tests inside a Docker container, you will need to alter your java jar run command to include a properly-configured -javaagent parameter (see http://www.eclemma.org/jacoco/trunk/doc/agent.html). After you do this, you will be able to use a jacoco report step to load the .exec file and convert it to HTML output.
Cool that this works for you :)
If you don't mind I'll close the issue, should be findable via search engine still anyway.
One more note for pilgrims coming from search engines:
Sometimes, files created inside a shared volume end up being owned by root on the host box. If you run into this, then I recommend doing the following:
-javaagent:./target/dependency/org.jacoco.agent-0.7.9-runtime.jar=append=true,output=tcpserver,address=*
<port>${docker.app.jacoco}:6300</port>
<execution>
<id>post-integration-test-dump</id>
<phase>post-integration-test</phase>
<goals>
<goal>dump</goal>
</goals>
<configuration>
<destFile>${project.build.directory}/coverage-reports/jacoco-it.exec</destFile>
<port>${docker.app.jacoco}</port>
<dump>true</dump>
</configuration>
</execution>
Hi @who I have added javaagent parameter to JAVA_OPT in docker file
environment:
JAVA_OPTS=javaagent:/opt/wildfly/jacocoagent.jar=destfile=/tmp/jacoco/jacoco.exec,output=tcpserver,address=*
I have checked port 6300 it's being listen but I can not see the file jacoco.exec create. Do I need to do anything, or the file is created right after the time I start the container?
I container I have that folder with full permission to write file, but I don't know why it still not appear
Hi @jasminecrazy , you need to take the dump to the file and generate report later. Below is the command you should use.
JAVA_OPTS=javaagent:/opt/wildfly/jacocoagent.jar=destfile=/tmp/jacoco/jacoco.exec,output=file,append=true
After you stop the container, file will be generated. Copy the jacoco.exec file and use jacocoagent.jar to generate html result.
Most helpful comment
One more note for pilgrims coming from search engines:
Sometimes, files created inside a shared volume end up being owned by root on the host box. If you run into this, then I recommend doing the following: