Cobertura throws a ClassNotFoundException

When creating my project using maven using JUnit-Tests with confidence and Cobertura to get test coverage, as a rule, everything worked fine. But when I recently added an exception that could be thrown (and thrown out) by some tests, maven always told me:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test (default-test) on project backend-server: Execution default-test of goal org.apache.maven.plugins:maven-surefire-plugin:2.18.1:test failed: There was an error in the forked process [ERROR] java.lang.NoClassDefFoundError: de/unileipzig/irpsim/backend/simulation/TimerowTooShortException [ERROR] at java.lang.Class.getDeclaredMethods0(Native Method) [ERROR] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701) [ERROR] at java.lang.Class.privateGetMethodRecursive(Class.java:3048) [ERROR] at java.lang.Class.getMethod0(Class.java:3018) [ERROR] at java.lang.Class.getMethod(Class.java:1784) [ERROR] at org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:57) [ERROR] at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:64) [ERROR] at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:59) [ERROR] at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:54) [ERROR] at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:52) [ERROR] at org.apache.maven.surefire.util.DefaultScanResult.applyFilter(DefaultScanResult.java:97) [ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:222) [ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:107) [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203) [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155) [ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103) [ERROR] Caused by: java.lang.ClassNotFoundException: de.unileipzig.irpsim.backend.simulation.TimerowTooShortException [ERROR] at java.net.URLClassLoader.findClass(URLClassLoader.java:381) [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:424) [ERROR] at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) [ERROR] at java.lang.ClassLoader.loadClass(ClassLoader.java:357) [ERROR] ... 16 more [ERROR] -> [Help 1] 

Running with -X or such things, unfortunately, did not help, and also see http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html or Intermittent NoClassDefFoundError when starting the maven / build surefire in jenkins did not give any useful hint of this problem.

My faithful plugin is defined as follows:

 <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <forkedProcessTimeoutInSeconds>900</forkedProcessTimeoutInSeconds> <argLine>-Djava.library.path=${nativelib.directory}</argLine> <excludes> <exclude>**/DatabaseTest.java</exclude> </excludes> </configuration> </plugin> 

When deactivating forking, everything starts to work fine, but native-libs are not enabled, so this is not an option. Also, when I exclude all tests that use a TimerowTooShortException , everything works fine.

After some attempts, I found that coobertur, even when the test is run with -Dcobertura.skip=true , is causing a problem. Cobertura was defined in plugins as follows:

 <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.6</version> <configuration> <formats> <format>xml</format> </formats> </configuration> <dependencies> <dependency> <groupId>org.ow2.asm</groupId> <artifactId>asm</artifactId> <version>5.0.3</version> </dependency> </dependencies> <executions> <execution> <phase>package</phase> <goals> <goal>cobertura</goal> </goals> </execution> </executions> </plugin> 

Unfortunately, I could not find any information about this particular problem when using surefire and cobertura with the expected exception. Does anyone know why this happens when using cobertura? And is there a workaround for this?

+5
source share
3 answers

I had a similar error with a multi-module project. Whenever I added -Dcobertura.skip=true , I got NoClassDefFound and ClassNotFound exceptions.

Unfortunately, I could not understand why. I suppose that not every coberra target accepts a parameter.

However, I "solved" the problem using profiles. That is, I moved the cobertura plugin from the default build configuration to the cobertura profile.

 <profiles> <profile> <id>cobertura-run</id> <properties> <maven.test.skip>false</maven.test.skip> </properties> <build> <plugins> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>cobertura-maven-plugin</artifactId> <version>2.7</version> <configuration> <formats> <format>xml</format> </formats> </configuration> <executions> <execution> <phase>package</phase> <goals> <goal>cobertura</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </profile> </profiles> 

Whenever I want to start cobertura, I use -P cobertura-run instead of disabling or enabling cobertura on the command line.

+3
source

There is a problem with open coberra about this https://github.com/mojohaus/cobertura-maven-plugin/issues/8

+2
source

I had the same error but adding

 `<testFailureIgnore>false</testFailureIgnore>` 

solve the problem of. Therefore, the following configuration should solve the problem.

  <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>2.18.1</version> <configuration> <skipTests>${skipTests}</skipTests> <testFailureIgnore>false</testFailureIgnore> <includes> <include>**/*.java</include> </includes> </configuration> </plugin> 
0
source

All Articles