当mvn安装失败时 -

时间:2012-11-07 16:15:44

标签: java maven junit install classloader

我有一个带有单元测试的maven项目,在运行" mvn install"时会得到一个大的异常跟踪。令人惊讶的是 - 这个堆栈跟踪实际上并没有导致任务失败!它似乎与Junit库的可用性有关......

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

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

3)为什么Maven说"建立成功"很明显,万无一失的实用程序引发了一个令人讨厌的异常?

  

org.apache.maven.surefire.util.SurefireReflectionException:   java.lang.reflect.InvocationTargetException;嵌套异常是   java.lang.reflect.InvocationTargetException:null   java.lang.reflect.InvocationTargetException at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   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(Native Method)at   java.lang.Class.privateGetDeclaredMethods(Class.java:2427)at at   java.lang.Class.getMethod0(Class.java:2670)at   java.lang.Class.getMethod(Class.java:1603)at   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)at   java.security.AccessController.doPrivileged(Native Method)at   java.net.URLClassLoader.findClass(URLClassLoader.java:190)at   java.lang.ClassLoader.loadClass(ClassLoader.java:306)at   sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:301)at at   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>

2 个答案:

答案 0 :(得分:3)

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

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

2)DID运行的测试实际上正在进行一些JVM黑客攻击,使用类似&#34; Unsafe&#34; - 导致分段错误。这种分段故障与整个Maven构建相混淆,破坏了maven输出的结果。

带回家的课程是:

1)(不是100%肯定,但它出现了) - 如果在mvn构建期间在JVM中发生了一些奇怪的低级别故障,那么可能会在结束时发现奇怪的结果,而不是简单地指出正确的错误/失败方式

2)针对surefire行为的默认Junit测试用例不会自动运行包中的所有@Test方法 - 必须正确命名类,或者必须手动编辑surefire模式过滤器。

答案 1 :(得分:0)

我通过TestNG集成测试(故障安全)遇到了这样的问题。一个类似的非常神秘的错误导致我失去了一整天(唉! - 请,Eclipse / TestNG,如果你要给我们错误,让他们有所帮助)。在我的情况下,这是因为测试方法的访问者是私有的,需要公开。

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

相关问题