SKM IT World

Just another blog about IT


Leave a comment

Maven Project Setup for Mixing Spock 1.x and JUnit 5 Tests

I create a sample Groovy project for Maven, that mixes Spock tests and JUnit 5 tests in one project. In the next section I’ll describe how to set up such kind of Maven project.

Enable Groovy in the Project

First at all, you have to enable Groovy in your project. One possibility is to add the GMavenPlus Plugin to your project.

<build&gt;
    <plugins&gt;
        <plugin&gt;
            <groupId&gt;org.codehaus.gmavenplus</groupId&gt;
            <artifactId&gt;gmavenplus-plugin</artifactId&gt;
            <version&gt;1.6.2</version&gt;
            <executions&gt;
                <execution&gt;
                    <goals&gt;
                        <goal&gt;addSources</goal&gt;
                        <goal&gt;addTestSources</goal&gt;
                        <goal&gt;compile</goal&gt;
                        <goal&gt;compileTests</goal&gt;
                    </goals&gt;
                </execution&gt;
            </executions&gt;
        </plugin&gt;
    </plugins&gt;
</build&gt;

The goals addSources and addTestSources add Groovy (test) sources to Maven’s main (test) sources. The default locations are src/main/groovy (for main source) and src/test/groovy (for test source). Goals compile and compileTests compile the Groovy (test) code. If you don’t have Groovy main code, you can omit addSource and compile.

This above configuration is always using the latest released Groovy version. If you want to ensure that a specific Groovy version is used, you have to add the specific Groovy dependency to your classpath.

   <dependencies&gt;
        <dependency&gt;
            <groupId&gt;org.codehaus.groovy</groupId&gt;
            <artifactId&gt;groovy</artifactId&gt;
            <version&gt;2.5.6</version&gt;
        </dependency&gt;
  </dependencies&gt;

Enable JUnit 5 in the Project

The simplest setup for using JUnit 5 in your project is to add the JUnit Jupiter dependency in your test class path and to configure the correct version of Maven Surefire Plugin (at least version 2.22.0).

    <dependencies&gt;
<!--... maybe more dependencies --&gt;
        <dependency&gt;
            <groupId&gt;org.junit.jupiter</groupId&gt;
            <artifactId&gt;junit-jupiter</artifactId&gt;
            <scope&gt;test</scope&gt;
        </dependency&gt;
    </dependencies&gt;

    <dependencyManagement&gt;
        <dependencies&gt;
            <dependency&gt;
                <groupId&gt;org.junit</groupId&gt;
                <artifactId&gt;junit-bom</artifactId&gt;
                <version&gt;${junit.jupiter.version}</version&gt;
                <scope&gt;import</scope&gt;
                <type&gt;pom</type&gt;
            </dependency&gt;
        </dependencies&gt;
    </dependencyManagement&gt;
    <build&gt;
        <plugins&gt;
        <!-- other plugins --&gt;
            <plugin&gt;
                <artifactId&gt;maven-surefire-plugin</artifactId&gt;
                <version&gt;2.22.1</version&gt;
            </plugin&gt;
        </plugins&gt;
    </build&gt;

Enable Spock in the Project

Choosing the right Spock dependency depends on which Groovy version you are using in the project. In our case, a Groovy version 2.5. So we need Spock in version 1.x-groovy-2.5 in our test class path.

    <dependencies&gt;
        <!-- more dependencies --&gt;
        <dependency&gt;
            <groupId&gt;org.spockframework</groupId&gt;
            <artifactId&gt;spock-core</artifactId&gt;
            <version&gt;1.3-groovy-2.5</version&gt;
            <scope&gt;test</scope&gt;
        </dependency&gt;
    </dependencies&gt;

Now the expectation is that the Spock tests and the JUnit5 tests are executed in the Maven build. But only the JUnit5 tests are executed by Maven. So what happened?

I started to change the Maven Surefire Plugin version to 2.21.0. Then the Spock tests were executed, but no JUnit5 tests. The reason is that in the version 2.22.0 of Maven Surefire Plugin JUnit4 provider is replaced by JUnit Platform Provider as default. But Spock in version 1.x is based on JUnit4. This will be changed in Spock version 2. This version will be based on the JUnit5 Platform. Thus, for Spock 1.x, we have to add JUnit Vintage dependency to our test class path.

    <dependencies&gt;
        <!-- more dependencies --&gt;
          <dependency&gt;  <!--Only necessary for surefire to run spock tests during the maven build --&gt;
            <groupId&gt;org.junit.vintage</groupId&gt;
            <artifactId&gt;junit-vintage-engine</artifactId&gt;
            <scope&gt;test</scope&gt;
        </dependency&gt;
    </dependencies&gt;

This allows running elder JUnit (3/4) tests on the JUnit Platform. With this configuration both, Spock and JUnit 5 tests, are executed in the Maven build.

Links

Advertisements