There are many reasons for this problem. But most common case is that the JaCoCo agent is failed to configured into the command line. Using the command "mvn -X clean verify" to check on the debug logs. At the "TESTS" step, you will be able to see a line for "Forking command line".
If the JaCoCo agent is configured correctly, you will be able to see similar logs as below.
-------------------------------------------------------
T E S T S
-------------------------------------------------------
-------------------------------------------------------
T E S T S
-------------------------------------------------------
[DEBUG] boot classpath: /Users/jhuang8/.m2/raptor2/org/apache/maven/surefire/surefire-booter/2.17/surefire-booter-2.17.jar /Users/jhuang8/.m2/raptor2/org/apache/maven/surefire/surefire-api/2.17/surefire-api-2.17.jar /Users/jhuang8/Documents/workspace/JaCoCoPractice/target/test-classes /Users/jhuang8/Documents/workspace/JaCoCoPractice/target/classes /Users/jhuang8/.m2/raptor2/junit/junit/4.8.1/junit-4.8.1.jar /Users/jhuang8/.m2/raptor2/org/apache/maven/surefire/surefire-junit4/2.17/surefire-junit4-2.17.jar
[DEBUG] boot(compact) classpath: surefire-booter-2.17.jar surefire-api-2.17.jar test-classes classes junit-4.8.1.jar surefire-junit4-2.17.jar
Forking command line: /bin/sh -c cd /Users/jhuang8/Documents/workspace/JaCoCoPractice && /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/bin/java -javaagent:/Users/jhuang8/.m2/raptor2/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/Users/jhuang8/Documents/workspace/JaCoCoPractice/target/jacoco.exec -Xmx1024m -XX:MaxPermSize=512m -jar /Users/jhuang8/Documents/workspace/JaCoCoPractice/target/surefire/surefirebooter3359878682012651006.jar /Users/jhuang8/Documents/workspace/JaCoCoPractice/target/surefire/surefire8553706350128891199tmp /Users/jhuang8/Documents/workspace/JaCoCoPractice/target/surefire/surefire_04828513279405888630tmp
And how can we add in these JaCoCo agent info? according to the office site Jacobo:prepare-agent, you can obviously add in the @{argLine} at your maven-surefire-plugin configurations as
The default name is argLine, but sometime, the JaCoCo configuration will be overwrote by some other argLine setup or plugin in pom. A way to avoid this is to configure a new property name at JaCoCo plugin and put it at the maven-surefire-plugin. Below is the sample configruation.<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<forkMode>once</forkMode>
<forkCount>1</forkCount>
<argLine>@{argLine} -Xmx1024m </argLine>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
Set a new propertyName at JaCoCo plugin
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.7.6.201602180812</version>
<executions>
<execution>
<id>prepare-agent</id>
<goals>
<goal>prepare-agent</goal>
</goals>
<configuration>
<propertyName>jaCoCoArgLine</propertyName>
</configuration>
</execution>
<execution>
<id>post-unit-test</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
and put the new propertyName at maven-surefire-plugin like below
<argLine>jaCoCoArgLine -Xmx1024m </argLine>
[INFO] --- jacoco-maven-plugin:0.7.6.201602180812:prepare-agent (prepare-agent) @ JaCoCoPractice ---
[INFO] jaCoCoArgLine set to -javaagent:/Users/jhuang8/.m2/raptor2/org/jacoco/org.jacoco.agent/0.7.6.201602180812/org.jacoco.agent-0.7.6.201602180812-runtime.jar=destfile=/Users/jhuang8/Documents/workspace/JaCoCoPractice/target/jacoco.exec
If the problem still shows up, then double check the maven-surefire-plugin, according to official site Maven Plug-in it has the restrictions as below.
When using the maven-surefire-plugin or maven-failsafe-plugin you must not use a forkCount of 0 or set the forkMode to never as this would prevent the execution of the tests with the javaagentset and no coverage would be recorded.
Thanks for the post, but I found it necessary to make this change for it to work - @{jaCoCoArgLine} -Xmx1024m
ReplyDeleteThank you @Nomad. This really saved my day.
DeleteWow, thank you, Jane! Also, thanks Nomad! You solved my problem and I learned more in a few minutes than I did in an hour of grunt work!
ReplyDeleteWithout using maven or gradle how can it be done ? My jacoco.exec file is missing.
ReplyDelete