Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running spock unit tests with Maven

On a previous project I used the Spock testing framework to unit test my Java code. I found this really productive so I am trying to add Spock tests to my current project which uses Maven as its build tool (The previous project used Gradle). While I can get Maven to compile my Spock tests (using groovy-eclipse-compiler), I am unable to get Maven to run the tests.

I've made a simple example to demonstrate my problem with 2 files:

  • pom.xml
  • src/test/java/ASpec.groovy

Contents of pom.xml:

<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/maven-v4_0_0.xsd">     <modelVersion>4.0.0</modelVersion>     <groupId>my.group</groupId>     <artifactId>my-artifact</artifactId>     <version>0.1-SNAPSHOT</version>      <dependencies>         <dependency>             <groupId>junit</groupId>             <artifactId>junit</artifactId>             <version>4.10</version>         </dependency>             <dependency>             <groupId>org.codehaus.groovy</groupId>             <artifactId>groovy-all</artifactId>             <version>2.0.8</version>             <scope>test</scope>         </dependency>         <dependency>             <groupId>org.spockframework</groupId>             <artifactId>spock-core</artifactId>             <version>0.7-groovy-2.0</version>             <scope>test</scope>         </dependency>     </dependencies>      <build>         <plugins>             <plugin>                 <groupId>org.apache.maven.plugins</groupId>                 <artifactId>maven-compiler-plugin</artifactId>                 <version>3.1</version>                 <configuration>                     <compilerId>groovy-eclipse-compiler</compilerId>                 </configuration>                 <dependencies>                     <dependency>                         <groupId>org.codehaus.groovy</groupId>                         <artifactId>groovy-eclipse-compiler</artifactId>                         <version>2.8.0-01</version>                     </dependency>                     <dependency>                         <groupId>org.codehaus.groovy</groupId>                         <artifactId>groovy-eclipse-batch</artifactId>                         <version>2.1.8-01</version>                     </dependency>                 </dependencies>             </plugin>         </plugins>     </build> </project> 

Contents of ASpec.groovy:

import spock.lang.Specification  class ASpec extends Specification {      def "Test A"(){         // Always fail         expect: false     } } 

When I execute mvn clean test (or mvn clean install) I would expect my single unit test to be run and fail. While it is compiled, Maven does not run it. Does any one know how to run a Spock unit test from Maven (or if it is possible?)

(I have not put my test in a package to keep the example simple. Also I have put my groovy code in src/test/java to avoid configuring the example to pick up source files from an additional directory, again to keep the example as simple as possible.)

like image 223
Neil Stevens Avatar asked Aug 07 '14 15:08

Neil Stevens


People also ask

How do I run Spock test in eclipse?

Right click on the project > Properties > Java Build Bath > Add External Jars and add spock-core-0.6-groovy-1.8. jar and check if Groovy Libraries are there in Build Path or not. If not click on Add Library and select Groovy Runtime Libraries and restart Eclipse. Now you should be able to run.

How do I run a test case using Maven?

The Maven surefire plugin provides a test parameter that we can use to specify test classes or methods we want to execute. If we want to execute a single test class, we can execute the command mvn test -Dtest=”TestClassName”.

How can Junits be implemented using Maven?

The Maven Surefire Plugin 2.22. 0 (or newer) provides native support for JUnit 5. If we want to use the native JUnit 5 support of the Maven Surefire Plugin, we must ensure that at least one test engine implementation is found from the classpath. We can run our unit tests by using the command: mvn clean test.


1 Answers

This answer is purely supplemental to @PeterNiederwieser's answer. In it he mentions that you can configure the name pattern used by Surefire. Here is an example of what worked for me:

<plugin>     <groupId>org.apache.maven.plugins</groupId>     <artifactId>maven-surefire-plugin</artifactId>     <version>2.18</version>     <configuration>         <includes>             <!-- By default only files ending in 'Test' will be included, so also include support for Spock style naming convention -->             <!-- Oddly enough for Groovy files, *Spec.groovy does not work, but *Spec.java does -->             <include>**/*Test.java</include>             <include>**/*Spec.java</include>         </includes>     </configuration> </plugin> 

Source

As I mention in the comments, I'm not sure why **/*Spec.groovy didn't work, but I'm happy to be able to use the normal Spock convention here.

like image 136
mnd Avatar answered Sep 24 '22 02:09

mnd