Search code examples
mavenspockjunit5

How to configure the maven plugins to run spock and junit5 tests


I want to run run spock and junit5 tests in the same project. So I have made an example project with nothing under src/main but two tests under src/test/java/a/package/. One of the test is a junit5 and the other one is a spock test.

But after issuing mvn test verify only the JUnit test is executed but the spock test is not. They run nicely together in Intellij via "run all tests" so there is clearly some configuration issue I can not find.

Here is my maven pom:

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>maven.spock.junit</groupId>
    <artifactId>test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <poi.version>3.17</poi.version>
    </properties>

    <dependencies>

        <!-- test dependencies -->

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
            <scope>test</scope>
        </dependency>


        <!-- spock testing framework -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.spockframework</groupId>
            <artifactId>spock-core</artifactId>
            <version>1.0-groovy-2.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.codehaus.groovy</groupId>
            <artifactId>groovy-all</artifactId>
            <version>2.4.4</version>
            <scope>test</scope>
        </dependency>

        <!-- hamcrest matcher -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.4.3</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass></mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
            <plugin>
                <!-- mvn test - exclude integration tests -->
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                    <excludes>
                        <exclude>%regex[.*Fixme.*]</exclude>
                        <exclude>%regex[.*(Regression|Integration)Test.*]</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <!-- mvn verify - execute integration tests only -->
                <artifactId>maven-failsafe-plugin</artifactId>
                <version>2.22.1</version>
                <configuration>
                    <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US -Duser.timezone=UTC</argLine>
                    <includes>
                        <include>%regex[.*(Regression|Integration)Test.*]</include>
                    </includes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>integration-test</goal>
                            <goal>verify</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.gmavenplus</groupId>
                <artifactId>gmavenplus-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <sources>
                        <source>
                            <directory>src/test/java</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                        <source>
                            <directory>src/test/groovy</directory>
                            <includes>
                                <include>**/*.groovy</include>
                            </includes>
                        </source>
                    </sources>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>testCompile</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

    <profiles>
        <profile>
            <id>skip-unit-tests</id>
            <activation>
                <property>
                    <name>skipUnitTests</name>
                </property>
            </activation>
            <build>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-surefire-plugin</artifactId>
                        <configuration>
                            <skipTests>true</skipTests>
                            <argLine>-Duser.timezone=UTC</argLine>
                        </configuration>
                    </plugin>
                </plugins>
            </build>
        </profile>
    </profiles>

</project>

If I use the following surefire configuration it executes the spock test but not the junit one.

        <plugin>
            <!-- mvn test - exclude integration tests -->
            <artifactId>maven-surefire-plugin</artifactId>
            <version>2.22.1</version>
            <configuration>
                <useFile>false</useFile>
                <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                <includes>
                    <include>**/*</include>
                </includes>
            </configuration>
        </plugin>

Solution

  • It seems that maven gets confused with the dependencies so the junit dependencies need to go from the /project/dependencies into the surefire and failsafe plugin like so:

    <?xml version="1.0" encoding="UTF-8"?>
    <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/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>maven.spock.junit</groupId>
        <artifactId>test</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    
            <java.version>1.8</java.version>
            <maven.compiler.target>${java.version}</maven.compiler.target>
            <maven.compiler.source>${java.version}</maven.compiler.source>
    
            <groovy.version>2.5.2</groovy.version>
            <junit-platform.version>1.2.0</junit-platform.version>
            <jupiter.version>5.2.0</jupiter.version>
            <spock.version>1.2-groovy-2.5</spock.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.codehaus.groovy</groupId>
                <artifactId>groovy-all</artifactId>
                <version>${groovy.version}</version>
                <type>pom</type> <!-- required JUST since Groovy 2.5.0 -->
            </dependency>
    
            <dependency>
                <groupId>org.spockframework</groupId>
                <artifactId>spock-core</artifactId>
                <version>${spock.version}</version>
                <scope>test</scope>
            </dependency>
    
            <dependency>
                <groupId>org.junit.jupiter</groupId>
                <artifactId>junit-jupiter-api</artifactId>
                <version>${jupiter.version}</version>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <defaultGoal>verify</defaultGoal>
    
            <plugins>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                </plugin>
    
                <plugin>
                    <groupId>org.codehaus.gmavenplus</groupId>
                    <artifactId>gmavenplus-plugin</artifactId>
                    <version>1.6.1</version>
                    <configuration>
                        <testSources>
                            <testSource>
                                <directory>src/test/java</directory>
                                <includes>
                                    <include>**/*.groovy</include>
                                </includes>
                            </testSource>
                            <testSource>
                                <directory>src/test/groovy</directory>
                                <directory>src/test</directory>
                                <includes>
                                    <include>**/*.groovy</include>
                                </includes>
                            </testSource>
                        </testSources>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>addTestSources</goal>
                                <goal>generateTestStubs</goal>
                                <goal>compileTests</goal>
                                <goal>removeTestStubs</goal>
                                <goal>removeStubs</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.22.0</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.junit.platform</groupId>
                            <artifactId>junit-platform-surefire-provider</artifactId>
                            <version>${junit-platform.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.junit.jupiter</groupId>
                            <artifactId>junit-jupiter-engine</artifactId>
                            <version>${jupiter.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.junit.vintage</groupId>
                            <artifactId>junit-vintage-engine</artifactId>
                            <version>${jupiter.version}</version>
                        </dependency>
                    </dependencies>
    
                    <configuration>
                        <detail>true</detail>
                        <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US</argLine>
                        <includes>
                            <include>%regex[.*]</include>
                        </includes>
                        <excludes>
                            <exclude>%regex[.*Fixme.*]</exclude>
                            <exclude>%regex[.*(Regression|Integration)Test.*]</exclude>
                        </excludes>
                    </configuration>
                </plugin>
                <plugin>
                    <artifactId>maven-failsafe-plugin</artifactId>
                    <version>2.22.0</version>
                    <dependencies>
                        <dependency>
                            <groupId>org.junit.platform</groupId>
                            <artifactId>junit-platform-surefire-provider</artifactId>
                            <version>${junit-platform.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.junit.jupiter</groupId>
                            <artifactId>junit-jupiter-engine</artifactId>
                            <version>${jupiter.version}</version>
                        </dependency>
                        <dependency>
                            <groupId>org.junit.vintage</groupId>
                            <artifactId>junit-vintage-engine</artifactId>
                            <version>${jupiter.version}</version>
                        </dependency>
                    </dependencies>
                    <configuration>
                        <argLine>-Xms2g -Xmx2g -XX:MaxPermSize=500m -Dfile.encoding=UTF-8 -Duser.language=en -Duser.region=US -Duser.timezone=UTC</argLine>
                        <includes>
                            <include>%regex[.*(Regression|Integration)Test.*]</include>
                        </includes>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>integration-test</goal>
                                <goal>verify</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </build>
    </project>
    

    All the credit belong to SanderSmee - Thanks!! I was just so lucky that I have found your repo! https://github.com/SanderSmee/spock-jupiter/blob/master/pom.xml