niedziela, 27 października 2013

Spock, TestNG and Maven Surefire

If you want to run Spock and TestNG tests together in Maven on standard configuration you'd receive:

Configuring TestNG with: TestNGMapConfigurator
org.testng.TestNGException: 
Failure in JUnit mode for class com.XXX: could not create/run JUnit test suite: 
Cannot find JUnit method class junit.framework.TestSuite$1.warning
at org.testng.junit.JUnitTestRunner.runFailed(JUnitTestRunner.java:237)
at org.testng.junit.JUnitTestRunner.start(JUnitTestRunner.java:230)
at org.testng.junit.JUnitTestRunner.run(JUnitTestRunner.java:211)
at org.testng.TestRunner$1.run(TestRunner.java:667)
at org.testng.TestRunner.runWorkers(TestRunner.java:1178)
at org.testng.TestRunner.privateRunJUnit(TestRunner.java:698)
at org.testng.TestRunner.run(TestRunner.java:605)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1158)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1083)
at org.testng.TestNG.run(TestNG.java:999)
at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:91)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.executeMulti(TestNGDirectoryTestSuite.java:211)
at org.apache.maven.surefire.testng.TestNGDirectoryTestSuite.execute(TestNGDirectoryTestSuite.java:107)
at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:113)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

The solution is to replace your surefire plugin with the following in pom.xml:

<plugin>                                                              
    <groupId>org.apache.maven.plugins</groupId>                       
    <artifactId>maven-surefire-plugin</artifactId>                    
    <version>2.16</version>                                           
    <configuration>                                                   
        <excludes>                                                    
            <exclude>**/SpockTests/*Test.java</exclude>                     
        </excludes>                                                   
    </configuration>                                                  
    <executions>                                                      
        <execution>                                                   
            <id>integration-test</id>                                 
            <goals>                                                   
                <goal>test</goal>                                     
            </goals>                                                  
            <phase>test</phase>                                       
            <configuration>                                           
                <testNGArtifactName>none:none</testNGArtifactName>    
                <junitArtifactName>junit:junit-dep</junitArtifactName>
                <excludes>                                            
                    <exclude>none</exclude>                           
                </excludes>                                           
                <includes>                                            
                    <include>**/SpockTests/*Test.java</include>             
                </includes>                                           
            </configuration>                                          
        </execution>                                                  
    </executions>                                                     
</plugin>