Skip to content

Latest commit

 

History

History
284 lines (230 loc) · 11.3 KB

README.adoc

File metadata and controls

284 lines (230 loc) · 11.3 KB

Spock framework and JUnit 5, Unit and Integration tests separation, Parallel tests execution in Maven

Description

A simple Maven project combining Spock 2.4-M4-groovy-4.0 and JUnit 5.11.0 tests, parallel tests execution and separation between Unit and Integration tests.

Based on:

Dependencies Diagram

dependencies diagram

Project Structure

src
    main
        java - source code
        resources - application resources
    test
        integration - integration tests
        resources - test resources
        unit - unit test

Supported File Name Patterns

  • Java Unit test:

    1. *Test.java

    2. *TestCase.java

    3. *Tests.java

    4. Test*.java

  • Java Integration test:

    1. IT*.java

    2. *IT.java

    3. *ITCase.java

  • Spock/Groovy Unit test:

    1. *Spec.groovy

  • Spock/Groovy Integration test:

    1. *IS.groovy

Spock Parallel Execution

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>

JUnit 5 Parallel Execution

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)

Spock Configuration

runner {
    filterStackTrace false
    optimizeRunOrder true
    parallel {
        enabled true
        dynamic(2.0)
    }
}

Useful Resources

Optional Extensions

Maven Surefire Report Plugin

  • Documentation

  • Usage:

    1. mvn clean verify site

    2. 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>

JaCoCo Plugin

  • Documentation

  • Usage:

    1. mvn clean verify

    2. 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>