Spock framework and JUnit 5, Unit and Integration tests separation, Parallel tests execution in Maven
-
JDK 22
-
Apache Maven 3.9.9
src
main
java - source code
resources - application resources
test
integration - integration tests
resources - test resources
unit - unit test
-
Java Unit test:
-
*Test.java
-
*TestCase.java
-
*Tests.java
-
Test*.java
-
-
Java Integration test:
-
IT*.java
-
*IT.java
-
*ITCase.java
-
-
Spock/Groovy Unit test:
-
*Spec.groovy
-
-
Spock/Groovy Integration test:
-
*IS.groovy
-
Spock 2.0 is based on JUnit 5 Platform and supports Parallel Execution since version 2.0-M4.
To enable parallel execution set the runner.parallel.enabled
configuration to true
in Spock Configuration.
Refer to documentation for details on supported modes and for fine-tuning.
Note
|
1.3 versions
Spock 1.3-groovy-2.5 is based on JUnit 4 and doesn’t support parallel execution within a spec.
|
Tip
|
Forked Execution
For older Spock versions forked execution could be enabled with this configuration:
|
<plugins>
[...]
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId> // same for maven-failsafe-plugin
[...]
<configuration>
<forkCount>2C</forkCount>
<reuseForks>false</reuseForks>
[...]
</configuration>
</plugin>
[...]
</plugins>
Global settings:
src/test/resources/junit-platform.properties
junit.jupiter.execution.parallel.enabled=true
junit.jupiter.execution.parallel.config.strategy=dynamic
junit.jupiter.execution.parallel.config.dynamic.factor=2
junit.jupiter.execution.parallel.mode.default=concurrent
junit.jupiter.execution.parallel.mode.classes.default=concurrent
Fine tune per class or for single methods by annotation:
@Execution(ExecutionMode.CONCURRENT)
@Execution(ExecutionMode.SAME_THREAD)
Defined in:
src/test/resources/SpockConfig.groovy
runner {
filterStackTrace false
optimizeRunOrder true
parallel {
enabled true
dynamic(2.0)
}
}
-
junit5-jupiter-starter-maven - how to execute JUnit Jupiter tests using Maven
-
Writing Unit Tests With Spock Framework: Creating a Maven Project
-
JUnit 4 + Spock 2 (Groovy 2.5), JUnit 5 + Spock 2 (Groovy 2.5) - StackOverflow answer by kriegaex
-
Add migration guide Spock 1.x → 2.x to manual or separate document - Spock issue #1166
-
Usage:
-
mvn clean verify site
-
Open
target/site/index.html
in Web browser
-
-
Setup
<project>
[...]
<build>
[...]
<plugins>
[...]
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-site-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-site-plugin</artifactId>
<version>3.12.0</version>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-project-info-reports-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.3.0</version>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-pmd-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>3.17.0</version>
</plugin>
<!-- https://mvnrepository.com/artifact/org.apache.maven.plugins/maven-surefire-report-plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-report-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</reporting>
</project>
-
Usage:
-
mvn clean verify
-
Open
target/site/jacoco/index.html
in Web browser
-
-
Setup
<project>
[...]
<build>
[...]
<plugins>
[...]
<!-- https://mvnrepository.com/artifact/org.jacoco/jacoco-maven-plugin -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.8</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>