Using Cobertura's plug-ins in Maven projects to generate reports of Java code coverage, but in practice, it often appears that all 0% of the reported values are present in the documentation to illustrate how to resolve the issue.
All issues with coverage of 0% are related to the plug-in surefire that run unit tests, and the various problems that arise are due to the forkcount and reuseforks of the plugin's settings.
If this problem occurs, configure the Surefire plug-in according to the instructions and requirements below.
Surefire Plugin Official Description: https://maven.apache.org/surefire/maven-surefire-plugin/examples/fork-options-and-parallel-execution.html
The
1:cobertura plugin bug caused the report to be 0 when the first report was generated when the configuration item in Maven-surefire-plugin forkconut to 0 o'clock.
Bug url:http://jira.codehaus.org/browse/mcobertura-70
Solution:
1. Run two times mvn cobertura:cobertura command, in Jenkins is the configuration run two times the command.
2. Set Forkcount = 1 To modify the reference:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>x.x.x</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>true</reuseForks>
</configuration>
</plugin>
Ps:
1. This is the default configuration, which is the value directly from adq-parent or directly using the plug-in.
2. In addition, regarding the FORKCOUNT>0 Setup may cause the unit test to run on more than one thread, resulting in not sequential execution, I queried the official website description, the original is:
The default setting is Forkcount=1/reuseforks=true, which means a surefire creates one new JVM process to execute all t ESTs in one MAVEN module.
Therefore, if there is only one module in the project, all tests should be run in the same thread, so setting it to 1 is also possible.
Scenario 2: When a unit test case has more content or a reference to the external LIB, it can cause a memory overflow error in the plug-in Cobertura runtime (there will be a java.lang.OutOfMemoryError error in the log), which causes the write code coverage data to fail. Edit solution: First of all, it is necessary to indicate that there will be an oom error in the log.
1. Modify Reuseforks = False, note that at this point Surefire will start a separate JVM for each test class to run the unit tests, although the problem is avoided, but if there is a dependency between unit tests, this cannot be resolved. This also results in a significant increase in the time to run the tests, modifying the references:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>x.x.x</version>
<configuration>
<forkCount>1</forkCount>
<reuseForks>false</reuseForks>
</configuration>
</plugin>
2. If the first workaround is not applicable, you can use this method to increase the startup parameters for the starting JVM,-xms512m-xmx1024m-xx:maxpermsize=128m, and set boot memory to resolve. Note that in JDK1.8, MaxPermSize has been removed and a new mechanism has been introduced that should not occur again, so if you modify this value, thejdk is removed after upgrading to 1.8 , and the following is a modified reference:
<groupid>org.apache.maven.plugins</groupId>
<artifactid>maven-surefire-plugin</artifactId>
<version> version Let's see, ~</version>.
<configuration>
<argLine>-Xms512m-Xmx1024m-XX:MaxPermSize=128m</argLine>
</configuration>
Resolves an issue where plug-in Cobertura generated code coverage is reported as 0%