2013-03-20 299 views
9

我在使用JUnit,Groovy,Spock和Maven時並行執行JUnit測試時出現問題。當執行他們,我得到下面的測試之後,成功地通過了:JUnit + Maven +並行測試執行錯誤

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 18.362s 
[INFO] Finished at: Wed Mar 20 15:14:25 CET 2013 
[INFO] Final Memory: 16M/221M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test (default-test) on project spock-webdriver: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: There was an error in the forked process 
[ERROR] java.lang.NoSuchMethodError: org.apache.maven.surefire.common.junit4.JUnit4RunListener.rethrowAnyTestMechanismFailures(Lorg/junit/runner/Result;)V 
[ERROR] at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:129) 
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
[ERROR] at java.lang.reflect.Method.invoke(Method.java:601) 
[ERROR] at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208) 
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158) 
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95) 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

當使能在我的pom.xml使用JUnit 47提供給力,我得到:

[INFO] ------------------------------------------------------------------------ 
[INFO] BUILD FAILURE 
[INFO] ------------------------------------------------------------------------ 
[INFO] Total time: 3.271s 
[INFO] Finished at: Wed Mar 20 15:31:42 CET 2013 
[INFO] Final Memory: 17M/178M 
[INFO] ------------------------------------------------------------------------ 
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.14:test (default-test) on project spock-webdriver: ExecutionException; nested exception is java.util.concurrent.ExecutionException: java.lang.RuntimeException: There was an error in the forked process 
[ERROR] java.lang.NoSuchMethodError: org.apache.maven.surefire.util.TestsToRun.size()I 
[ERROR] at org.apache.maven.surefire.junitcore.JUnitCoreProvider.invoke(JUnitCoreProvider.java:123) 
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
[ERROR] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
[ERROR] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
[ERROR] at java.lang.reflect.Method.invoke(Method.java:601) 
[ERROR] at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208) 
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158) 
[ERROR] at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 
[ERROR] at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95) 
[ERROR] -> [Help 1] 
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch. 
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoFailureException 

pom.xml中看起來是這樣的

<dependencies> 
     <dependency> 
      <groupId>org.seleniumhq.selenium</groupId> 
      <artifactId>selenium-java</artifactId> 
      <version>2.31.0</version> 
      <scope>compile</scope> 
     </dependency> 

     <!--If this is commented out parallel testing is not working!!! When in testexecution itself is not working --> 
     <dependency> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>2.14</version> 
     </dependency> 

     <dependency> 
      <groupId>org.spockframework</groupId> 
      <artifactId>spock-maven</artifactId> 
      <version>0.7-groovy-2.0</version> 
      <scope>test</scope> 
     </dependency> 
    </dependencies> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.codehaus.gmaven</groupId> 
       <artifactId>gmaven-plugin</artifactId> 
       <version>1.3</version> 
       <configuration> 
        <providerSelection>1.7</providerSelection> 
        <source/> 
       </configuration> 
       <executions> 
        <execution> 
         <goals> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
       <dependencies> 
        <dependency> 
         <groupId>org.codehaus.gmaven.runtime</groupId> 
         <artifactId>gmaven-runtime-1.7</artifactId> 
         <version>1.3</version> 
         <exclusions> 
          <exclusion> 
           <groupId>org.codehaus.groovy</groupId> 
           <artifactId>groovy-all</artifactId> 
          </exclusion> 
         </exclusions> 
        </dependency> 
        <dependency> 
         <groupId>org.codehaus.groovy</groupId> 
         <artifactId>groovy-all</artifactId> 
         <version>2.0.6</version> 
        </dependency> 
       </dependencies> 
      </plugin> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>2.14</version> 
       <dependencies> 
       </dependencies> 
       <configuration> 
        <includes> 
         <include>**/*Test.java</include> 
        </includes> 
        <!-- Surefire 2.14+ configuration for parallelization --> 
        <forkCount>5</forkCount> 
        <reuseForks>false</reuseForks> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
+0

如果您啓動測試順序會發生什麼?使用另一個版本的'junit','surefire'? – 2013-03-20 14:37:27

+0

沒有太多取決於Surefire版本我得到例如2.12.2 '無法執行目標org.apache.maven.plugins:maven-surefire-plugin:2.12.2:項目測試(默認測試)spock-webdriver :執行目標爲org.apache.maven.plugins的默認測試:maven-surefire-plugin:2.12.2:測試失敗:分叉的虛擬機終止而沒有正確地說再見。虛擬機崩潰或System.exit調用? - > [Help 1]' – 2013-03-20 15:16:05

+0

在添加spock依賴後立即崩潰。我嘗試了硒和junit依賴,並建立它。添加spock依賴到我的pom後,它不起作用。嘗試了幾個spock版本 – 2013-03-20 15:49:50

回答

1

嘗試使用2.9版本或以上

<groupId>org.apache.maven.plugins</groupId> 
<artifactId>maven-surefire-plugin</artifactId> 
<version>2.9</version> 
0

我不確定這是否相關,但當我在Maven中運行我的Grails Spock測試時,我遇到了「分支進程中出現錯誤」錯誤。原來,我在我的文件名缺少一個「S」:

MyCoolTest.groovy => MyCoolTests.groovy

6

這是一個古老的線程,但爲別人誰可能碰到這個問題......這錯誤是由於與surefire插件依賴和spock 0.7.0依賴不一致而導致的。 Spock使用surefire-junit47,2.11版本。這個類/方法,JUnit4RunListener.rethrowAnyTestMechanismFailures在那個surefire版本中不存在。解決的辦法是排除來自斯波克依賴的定義是這樣的神火版本:

<dependency> 
    <groupId>org.spockframework</groupId> 
    <artifactId>spock-maven</artifactId> 
    <version>0.7-groovy-2.0</version> 
    <exclusions> 
     <exclusion> <!-- exclude so we can pick up surefire-junit4 2.17 version for failsafe integration tests --> 
      <groupId>org.apache.maven.surefire</groupId> 
      <artifactId>surefire-junit47</artifactId> 
     </exclusion> 
    </exclusions> 
</dependency> 

現在,它將使用萬無一失的版本,你已經定義,完整性,這裏的神火依賴我用:

<dependency> 
    <groupId>org.apache.maven.surefire</groupId> 
    <artifactId>surefire-junit4</artifactId> 
    <version>2.17</version> 
</dependency> 

然後確保插件定義允許單元測試和集成測試單獨進行(這部分更多的是關於獲得單元測試,集成測試,spock,maven和groovy玩的很好)。

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-surefire-plugin</artifactId> 
    <version>2.17</version> 
    <dependencies> 
     <dependency> 
      <groupId>org.apache.maven.surefire</groupId> 
      <artifactId>surefire-junit4</artifactId> 
      <version>2.17</version> 
     </dependency> 
    </dependencies> 
    <configuration> 
     <forkCount>0</forkCount> 
     <includes> 
      <include>%regex[.*Test.*]</include> 
     </includes> 
     <excludes> 
      <exclude>%regex[.*IntegrationTest.*]</exclude> 
     </excludes> 
    </configuration> 
</plugin> 

<plugin> 
    <groupId>org.apache.maven.plugins</groupId> 
    <artifactId>maven-failsafe-plugin</artifactId> 
    <version>2.17</version> 
    <configuration> 
     <includes> 
      <include>%regex[.*IntegrationTest.*]</include> 
     </includes> 
    </configuration> 
    <executions> 
     <execution> 
      <id>failsafe-integration-tests</id> 
      <goals> 
       <goal>integration-test</goal> 
      </goals> 
     </execution> 
    </executions> 
</plugin> 

現在,單元測試將觸發與測試目標,測試和集成測試可以推出獨立於這樣的需求:MVN故障保護:集成測試

+0

這幫助我們解決了我們的問題。謝謝! – Steve 2014-09-23 13:08:22