【问题标题】:When mvn install fails cryptically -当 mvn install 神秘地失败时 -
【发布时间】:2012-10-27 17:52:34
【问题描述】:

我有一个带有单元测试的 maven 项目,在运行“mvn install”时我得到了一个很大的异常跟踪。令人惊讶的是 - 这个堆栈跟踪实际上不会导致任务失败!看来和Junit库的可用性有关……

1) 我想知道如何(显然)为这个项目解决这个问题,以便库可用并运行测试(是的,Junit4 在 pom.xml 依赖项中)。

2) 明确调试并找到根本原因的最佳方法是什么?

3) 为什么 Maven 说“构建成功”,而 surefire 实用程序显然抛出了一个讨厌的异常?

org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException;嵌套异常是 java.lang.reflect.InvocationTargetException:空 java.lang.reflect.InvocationTargetException 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) 在 java.lang.reflect.Method.invoke(Method.java:597) 在 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) 在 org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172) 在 org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70) 引起:java.lang.NoClassDefFoundError:测试在 java.lang.Class.getDeclaredMethods0(本机方法)在 java.lang.Class.privateGetDeclaredMethods(Class.java:2427) 在 java.lang.Class.getMethod0(Class.java:2670) 在 java.lang.Class.getMethod(Class.java:1603) 在 org.apache.maven.surefire.util.ReflectionUtils.tryGetMethod(ReflectionUtils.java:57) 在 org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:65) 在 org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:60) 在 org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:55) 在 org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:52) 在 org.apache.maven.surefire.util.DefaultDirectoryScanner.locateTestClasses(DefaultDirectoryScanner.java:80) 在 org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:174) 在 org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:83) ... 9 更多原因:java.lang.ClassNotFoundException:测试在 java.net.URLClassLoader$1.run(URLClassLoader.java:202) 在 java.security.AccessController.doPrivileged(Native Method) 在 java.net.URLClassLoader.findClass(URLClassLoader.java:190) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:306) 在 sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301) 在 java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 21 更多

POM 在下面

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>rudolf</groupId>
    <artifactId>r1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>r1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.1</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.3</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.derby</groupId>
            <artifactId>derby</artifactId>
            <version>10.9.1.0</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.4.0a</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.9</version>
            <executions>
                <execution>
                    <phase>test</phase>
                    <goals>
                        <goal>test</goal>
                    </goals>
                </execution>
            </executions>
          </plugin>          
          <plugin>
             <groupId>org.apache.maven.plugins</groupId>
             <artifactId>maven-compiler-plugin</artifactId>
             <version>2.3.2</version>
             <configuration>
               <source>1.6</source>
               <target>1.6</target>
             </configuration>
           </plugin>
        </plugins>
    </build>



    <pluginRepositories>
        <pluginRepository>
            <id>onejar-maven-plugin.googlecode.com</id>
            <url>http://onejar-maven-plugin.googlecode.com/svn/mavenrepo</url>
        </pluginRepository>
    </pluginRepositories>
</project>

【问题讨论】:

  • 你能告诉我们你的pom.xml吗?
  • 你是从命令行运行的吗?主题说 Eclipse。
  • 是的,好点 - 我修正了标题。
  • 这里是输出:pastebin.com/GaYLim2j
  • 您是否混合使用了 junit3 和 junit4 测试? (你有junit3 套件)?您可以尝试使用更高版本的 JUnit 吗?您使用的是 4.1。可以试试 4.10 吗?

标签: java maven junit installation classloader


【解决方案1】:

这绝对是一场完美的愚蠢风暴:

1) 我的测试类不是根据默认的肯定正则表达式命名的,请参阅相关的Maven does not find JUnit tests to run。所以测试并没有真正运行。

2) DID 运行的测试实际上是在进行一些 JVM 黑客攻击,使用诸如“不安全”之类的类 - 导致分段错误。这种分段错误与整个 Maven 构建相混淆,破坏了 Maven 输出的结果。

带回家的课程是:

1) (不是 100% 肯定,但看起来)——如果在 mvn 构建期间 JVM 中发生了一些奇怪的低级故障,人们可能会在最后看到奇怪的结果,这不仅仅表示正确的错误/故障方式

2) 用于安全行为的默认 Junit 测试用例不会自动运行包中的所有 @Test 方法 - 必须正确命名类,否则您必须手动编辑安全模式过滤器。

【讨论】:

  • 嗨@jayunit100:请告诉我为消除此错误所做的更改。因为我也遇到了与“构建成功”相同的错误。你能帮帮我吗?
  • 避免混合 junit 测试版本 3/4,如果可以的话,避免在单元测试中使用 jvm hack,否则,我不记得了
【解决方案2】:

我在使用 TestNG 集成测试(故障安全)时遇到了这样的问题。一个类似的非常神秘的错误导致我失去了一整天(啊!-请 Eclipse/TestNG,如果您要给我们错误,请让它们有所帮助)。就我而言,这是因为测试方法上的访问器是私有的,需要公开。

我希望其他人在浪费一整天之前发现这很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-01
    • 1970-01-01
    • 2011-01-20
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多