问题
After switching from maven 2 to maven 3 I have found out having 0% test coverage reported by cobertura. I've stated question about which versions of cobertura and surefire to use: What versions of cobertura and surefire plugins work together under maven3?
I have, however, investigated problem deeper and found out what of configuration fragment was not working:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>${surefire.plugin.version}</version>
<configuration>
<forkMode>never</forkMode>
<redirectTestOutputToFile>true</redirectTestOutputToFile>
<argLine>-Xmx512m -XX:MaxPermSize=256m</argLine>
</configuration>
</plugin>
After changing forkMode from never
to once
the test coverage started to be generated. So, the problem was not the plugins version incompatibility itself, but the problem with the support by various fork modes of surefire by cobertura under maven 3.
So, my question is, is it a bug, or cobertura plugin is designed in such way, that it won't work with some forkMode=never
?
回答1:
Cobertura is designed to output the coverage results when the JVM exits.
<forkMode>never</forkMode>
instructs Maven not to fork a JVM for running the tests but to re-use the current JVM.
In this case, the coverage results may not be output until Maven completes execution.
In Maven 2, I am not 100% certain, but I think some of how the forked lifecycle (evilly) used by the cobertura plugin worked caused either a JVM fork for the forked lifecycle, or else a classloader unload effectively had the same result.
Thus in my opinion it was a bug of Maven 2 that coverage happened to work with <forkMode>never</forkMode>
.
Note: <forkMode>never</forkMode>
is considered quite dangerous because of how System properties are not scoped per classloader, among other issues. <forkMode>once</forkMode>
is generally the best option (unless you have tests that abuse memory - some versions of JUnit keep all the test class instances in memory until reporting at the end of the run, so if each test class holds on to heavy objects, GC will be unable to clear them out as they are live until the end of the test run. In such cases a perclass/always forkMode will be required)
来源:https://stackoverflow.com/questions/11010428/is-the-surefires-forkmode-never-working-with-cobertura-under-maven-3