2017-05-06 55 views
1

我正在使用IntelliJ,Spring Boot 1.3,java8和maven。運行Spock單元測試以及TestNG在Spring引導基於maven的項目中

我們使用TestNG作爲我們的單元,但是當我開始使用Spock進行單元測試時,我非常喜歡使用Spock在Groovy中編寫測試。以下是文件夾結構:

SRC->測試 - >爪哇 - >基於TestNG的測試所有的Java住在這裏
SRC->測試 - >常規 - >所有常規基於斯波克測試住在這裏。所有測試都有一個後綴Spec,這就是我配置surefire插件來查看這些測試的方式。
現在,當我運行這些單獨的測試時,它們工作得很好。但是當我運行像mvn testmvn clean install這樣的maven生命週期命令時,即使啓用了maven編譯器和適當的庫,Spock測試也不會運行。

這是我的聚甲醛的樣子:

<plugin> 
       <groupId>org.codehaus.gmavenplus</groupId> 
       <artifactId>gmavenplus-plugin</artifactId> 
       <version>1.5</version> 
       <executions> 
        <execution> 
         <goals> 
          <goal>compile</goal> 
          <goal>testCompile</goal> 
         </goals> 
        </execution> 
       </executions> 
      </plugin> 
<plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-surefire-plugin</artifactId> 
       <version>${version.surefire}</version> 
       <configuration> 
        <skipTests>false</skipTests> 
        <useFile>false</useFile> 
        <includes> 
         <include>**/*Spec.*</include> 
         <include>**/*Test.java</include> 
        </includes> 
       </configuration> 

      </plugin> 

      <dependency> 
     <groupId>org.spockframework</groupId> 
     <artifactId>spock-core</artifactId> 
     <version>1.0-groovy-2.4</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> 
     <groupId>cglib</groupId> 
     <artifactId>cglib-nodep</artifactId> 
     <version>RELEASE</version> 
     <scope>test</scope> 
    </dependency> 
    <dependency> <!-- enables mocking of classes without default 
     constructor (together with CGLIB) --> 
     <groupId>org.objenesis</groupId> 
     <artifactId>objenesis</artifactId> 
     <version>2.1</version> 
     <scope>test</scope> 
    </dependency>  

我覺得我缺少插件的東西這將讓行家運行斯波克使用TestNG一起Maven的生命週期命令的過程中。我想我在這裏錯過了微不足道的事情。有人可以在這裏給我一些關於應該在pom中添加什麼的指針,或者如果有人在github中有一個示例框架項目,我可以看看。謝謝。

+0

你在樣例中使用'**/* Spec.java'如何:https://github.com/spockframework/spock-example/blob/master/pom.xml – juherr

回答

1

我能夠得到這個工作。這是我更新的pom。

我有兩個問題:

-One運行TestNG的和Groovy測試作爲Maven的測試生命週期的一部分。這是通過在surefire插件中添加依賴關係實現的。見下面的pom。

- 修復後,我有一個問題Intellij抱怨類已經存在我的基於groovy的測試。這是由於在目標文件夾中生成了存根。爲此,我必須提供配置來覆蓋下面鏈接建議的那些目錄。

<plugin> 
      <groupId>org.codehaus.gmavenplus</groupId> 
      <artifactId>gmavenplus-plugin</artifactId> 
      <version>1.5</version> 
      <executions> 
       <execution> 
        <goals> 
         <goal>addTestSources</goal> 
         <goal>testGenerateStubs</goal> 
         <goal>testCompile</goal> 
         <goal>removeTestStubs</goal> 
        </goals> 
       </execution> 
      </executions> 
      <configuration> 
       <!--<This setting is for intellij bug. This override default location of groovy stubs. for more info 
       check this : https://youtrack.jetbrains.com/issue/IDEA-153779>--> 
       <stubsOutputDirectory>${project.build.directory}/generated-groovy-stubs</stubsOutputDirectory> 
       <testStubsOutputDirectory>${project.build.directory}/generated-groovy-test-stubs</testStubsOutputDirectory> 
      </configuration> 
     </plugin> 
     <plugin> 
      <groupId>org.apache.maven.plugins</groupId> 
      <artifactId>maven-surefire-plugin</artifactId> 
      <version>${version.surefire}</version> 
      <configuration> 
       <includes> 
        <include>**/*Spec*.groovy</include> 
        <include>**/*Test*.java</include> 
       </includes> 
       <properties> 
        <property> 
         <name>junit</name> 
         <value>false</value> 
        </property> 
       </properties> 
       <threadCount>1</threadCount> 
      </configuration> 
      <!--Below dependency let's surefire play nice with groovy test and testng tests--> 
      <dependencies> 
       <dependency> 
        <groupId>org.apache.maven.surefire</groupId> 
        <artifactId>surefire-junit47</artifactId> 
        <version>${version.surefire}</version> 
       </dependency> 
       <dependency> 
        <groupId>org.apache.maven.surefire</groupId> 
        <artifactId>surefire-testng</artifactId> 
        <version>${version.surefire}</version> 
       </dependency> 
      </dependencies> 
     </plugin> 

我希望這可以幫助其他人試圖一起運行TestNG和Spock測試。

相關問題