--maven project --Slight tests were written in JUnit --It's hard to keep writing tests in JUnit --I want to write a test with Spock
――I want to add Spock while keeping the existing JUnit --I want to execute JUnit and Spock with one command (`` `./mvnw test```) --I don't mention how great spock is
It is a story. Dedicated to those who are still testing with JUnit in Maven ...
Add dependency to pom.xml
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>1.3-groovy-2.5</version>
<scope>test</scope>
</dependency>
Write a test
package jp.co.trech
import spock.lang.*
class AppSpec extends Specification {
def 'Sample test'() {
expect:
Math.max(1, 2) != 2 //I want to check if the test is running, so I dare to make it a failing test
}
}
Try running a test
./mvnw test
→ BUILD SUCCESS
→ No compiled file is generated & Spock is not running
Maven-compiler-plugin does not seem to work unless it is 3.6.2 or higher (see Groovy Eclipse Maven plugin) )
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
<compilerId>groovy-eclipse-compiler</compilerId>
<verbose>true</verbose>
<compilerArguments>
<indy/><!-- optional; supported by batch 2.4.12-04+ -->
<configScript>config.groovy</configScript><!-- optional; supported by batch 2.4.13-02+ -->
<!-- consult Lombok docs to confirm agent class name for your version -->
<javaAgentClass>lombok.launch.Agent</javaAgentClass>
</compilerArguments>
<fork>true</fork>
</configuration>
<dependencies>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-compiler</artifactId>
<version>3.3.0-01</version>
</dependency>
<dependency>
<groupId>org.codehaus.groovy</groupId>
<artifactId>groovy-eclipse-batch</artifactId>
<version>2.5.6-01</version>
</dependency>
<dependency><!-- optional;Add if you are using lombok in your project-->
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
Try running a test
./mvnw test
→ BUILD SUCCESS
→ A compiled file has been generated! → But Spock does not run
Add the following after the above maven-compiler-plugin
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M1</version>
<configuration>
<failIfNoTests>true</failIfNoTests>
<includes>
<include>**/*Test.*</include>
<include>**/*Spec.*</include>
</includes>
</configuration>
<dependencies>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit47</artifactId>
<version>3.0.0-M1</version>
</dependency>
</dependencies>
</plugin>
The version of maven-surefire-plugin depends on the version of java, so see Maven Surefire Plugin
Try running a test
./mvnw test
→ BUILD FAILURE
→ A compiled file has been generated! → Spock was also executed, so the test failed!
→ Modify the Spock test and if the test passes, you're done!
--Spock can be easily put in gradle, but it is difficult to add Spock to maven ――Let's use gradle quietly
Compiling Groovy maven-surefire-plugin Using JUnit groovy/groovy-eclipse Groovy Eclipse Maven plugin
Recommended Posts