Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cobertura causes ClassNotFoundException

When building my project with maven using JUnit-Tests with surefire and Cobertura to get test coverage, normally, everything worked fine. But when I recently added an exception that could be thrown (and is excepted) 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]

A run with -X or things like this unfortunately did not help, and also looking at http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.html or Intermittent NoClassDefFoundError when running a maven/surefire build in jenkins did not offer any usefull hint for this issue.

My surefire-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 the forking, everything begins working fine, but the native-libs are not included, so this is not an option. Also, when I exclude all tests that use the TimerowTooShortException, everything works fine.

After some tries, I found out that cobertura, even when the test is run with -Dcobertura.skip=true, is causing the problem. Cobertura was defined in the 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 specific problem when using surefire and cobertura together with an expected exception. Does somebody know why this happens when using cobertura? And is there a workaround for this?

like image 948
David Georg Reichelt Avatar asked Apr 10 '15 16:04

David Georg Reichelt


2 Answers

I experienced a similar error with a multimodule project. Whenever I added the -Dcobertura.skip=true I got the NoClassDefFound and ClassNotFound Exceptions.

Unfortunatly I could not figure out why. My guess is, that not every cobertura-goal takes the 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 cobertura to run, I use -P cobertura-run instead of disabling or enabling cobertura in commandline.

like image 122
aubium77 Avatar answered Oct 23 '22 10:10

aubium77


There is an open cobertura issue about it https://github.com/mojohaus/cobertura-maven-plugin/issues/8

like image 2
Jakub Bochenski Avatar answered Oct 23 '22 08:10

Jakub Bochenski