2012-11-07 86 views
1

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

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

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

3)为什么Maven的说“打造成功”时,明确了万无一失的工具扔了一个讨厌的例外?

org.apache.maven.surefire.util.SurefireReflectionException: java.lang.reflect.InvocationTargetException; nested exception is java.lang.reflect.InvocationTargetException: null java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:597) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:164) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:110) at org.apache.maven.surefire.booter.SurefireStarter.invokeProvider(SurefireStarter.java:172) at org.apache.maven.surefire.booter.SurefireStarter.runSuitesInProcessWhenForked(SurefireStarter.java:104) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:70) Caused by: java.lang.NoClassDefFoundError: Test at java.lang.Class.getDeclaredMethods0(Native Method) at java.lang.Class.privateGetDeclaredMethods(Class.java:2427) 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) at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isSuiteOnly(JUnit3TestChecker.java:65) at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.isValidJUnit3Test(JUnit3TestChecker.java:60) at org.apache.maven.surefire.common.junit3.JUnit3TestChecker.accept(JUnit3TestChecker.java:55) at org.apache.maven.surefire.common.junit4.JUnit4TestChecker.accept(JUnit4TestChecker.java:52) at org.apache.maven.surefire.util.DefaultDirectoryScanner.locateTestClasses(DefaultDirectoryScanner.java:80) at org.apache.maven.surefire.junit4.JUnit4Provider.scanClassPath(JUnit4Provider.java:174) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:83) ... 9 more Caused by: java.lang.ClassNotFoundException: Test at 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 java.lang.ClassLoader.loadClass(ClassLoader.java:247) ... 21 more

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

你能告诉我们你的'pom.xml'吗? – maba

+0

你是否从命令行运行?该主题说Eclipse。 –

+0

是的,好点 - 我确定了标题。 – jayunit100

回答

2

这是definetly愚蠢的完美风暴:

1)我的测试类是不按默认万无一失的正则表达式见命名相关Maven does not find JUnit tests to run。所以测试并没有真正运行。

2)这并运行测试实际上是做一些JVM黑客,使用类,如“不安全” - 导致分割故障。这种分段错误会随着Maven的整体构建而消失,从而破坏Maven输出的结果。

的带回家的教训是:

1)(不是100%肯定,但它似乎) - 如果MVN构建过程中发生JVM一些奇怪的低水平失败,人们可能预期奇怪的结果在结束时,不只是以正确的方式指示错误/失败

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

+0

嗨@ jayunit100:请你让我知道做了什么样的改变来消除这个错误。 因为我得到了与“建立成功”相同的错误。你可以帮我吗。 – techGaurdian

+0

避免混JUnit测试版本的3/4,避免做单元测试JVM黑客如果可以的话,否则,我不记得 – jayunit100

+0

肯定。感谢您的信息。 – techGaurdian

0

我遇到了像TestNG集成测试(故障安全)这样的问题。一个类似的真正神秘的错误使我失去了一整天(唉! - 请,Eclipse/TestNG,如果你要给我们错误,让他们有用)。就我而言,这是因为测试方法的访问者是私人的,需要公开。

我希望别人在浪费一整天之前找到这个有用的东西。