Enable Spock testing on a Java Maven Project

Adding support for Spock testing to your Java maven project is quite an easy job. All you have to do is the following:

  • Add the gmaven plugin

    <plugin>
    

    <groupId>org.codehaus.gmaven</groupId> <artifactId>gmaven-plugin</artifactId> <version>1.4</version> <configuration> <providerSelection>2.0</providerSelection> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>testCompile</goal> </goals> </execution> </executions> <dependencies> <dependency> <groupId>org.codehaus.gmaven.runtime</groupId> <artifactId>gmaven-runtime-2.0</artifactId> <version>1.4</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.2.2</version> </dependency> </dependencies> </plugin `

  • Configure the maven surefire plugin
    &lt;plugin&gt;
        &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
        &lt;artifactId&gt;maven-surefire-plugin&lt;/artifactId&gt;
        &lt;version&gt;2.8.1&lt;/version&gt;
        &lt;configuration&gt;
            &lt;includes&gt;
                &lt;include&gt;**/*Test.class&lt;/include&gt;
                &lt;include&gt;**/*Spec.class&lt;/include&gt;
            &lt;/includes&gt;
        &lt;/configuration&gt;
        &lt;dependencies&gt;
            &lt;dependency&gt;
                &lt;groupId&gt;org.apache.maven.surefire&lt;/groupId&gt;
                &lt;artifactId&gt;surefire-junit4&lt;/artifactId&gt;
                &lt;version&gt;2.8.1&lt;/version&gt;
            &lt;/dependency&gt;
        &lt;/dependencies&gt;
    &lt;/plugin&gt;
    
  • Add some maven dependencies
    `<dependency>
        <groupId>org.spockframework</groupId>
        <artifactId>spock-core</artifactId>
        <version>0.7-groovy-2.0</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>2.2.2</version>
        <scope>test</scope>
    </dependency>

IntelliJ Java Project: Spoc configuration

  • Add the scr/test/groovy folder under your project structure.