【问题标题】:Cobertura causes ClassNotFoundExceptionCobertura 导致 ClassNotFoundException
【发布时间】:2015-04-10 16:24:49
【问题描述】:

当使用 maven 构建我的项目时,使用带有 surefire 和 Cobertura 的 JUnit-Tests 来获得测试覆盖率,通常,一切正常。但是当我最近添加了一个可能被某些测试抛出(并且被排除在外)的异常时,maven 总是告诉我:

[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]

不幸的是,使用 -X 或类似的东西运行并没有帮助,而且查看 http://maven.apache.org/surefire/maven-surefire-plugin/examples/class-loading.htmlIntermittent NoClassDefFoundError when running a maven/surefire build in jenkins 并没有为这个问题提供任何有用的提示。

我的surefire-plugin定义如下:

<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>

当停用分叉时,一切都开始正常工作,但不包括本机库,所以这不是一个选项。此外,当我排除所有使用 TimerowTooShortException 的测试时,一切正常。

经过一些尝试,我发现即使使用-Dcobertura.skip=true 运行测试,cobertura 也会导致问题。 Cobertura 在插件中的定义如下:

<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>

不幸的是,在使用 surefire 和 cobertura 以及预期的异常时,我找不到有关此特定问题的任何信息。有人知道为什么在使用 cobertura 时会发生这种情况吗?有没有解决方法?

【问题讨论】:

    标签: java maven maven-cobertura-plugin


    【解决方案1】:

    我在多模块项目中遇到了类似的错误。每当我添加 -Dcobertura.skip=true 时,我都会遇到 NoClassDefFound 和 ClassNotFound 异常。

    不幸的是,我不知道为什么。我的猜测是,并非每个 cobertura-goal 都接受参数。

    但是,我使用配置文件“解决了”问题。也就是说,我将 cobertura 插件从默认构建配置移至 cobertura 配置文件。

    <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>
    

    每当我想让 cobertura 运行时,我都会使用 -P cobertura-run 而不是在命令行中禁用或启用 cobertura。

    【讨论】:

      【解决方案2】:

      有一个关于它的公开 cobertura 问题https://github.com/mojohaus/cobertura-maven-plugin/issues/8

      【讨论】:

        【解决方案3】:

        我有同样的错误,但添加了

        `<testFailureIgnore>false</testFailureIgnore>`
        

        解决问题。 所以接下来的配置应该可以解决问题。

                <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>
        

        【讨论】:

          猜你喜欢
          • 2012-04-13
          • 1970-01-01
          • 1970-01-01
          • 2015-04-19
          • 1970-01-01
          • 1970-01-01
          • 2021-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多