Sergey Pauk Sergey Pauk - 1 month ago 8
Groovy Question

Create and jump to Spock unit test for a Java class in Idea IDE

Is there a way to create and jump to Spock unit test for an application written in Java in Idea IDE (it's working fine for a Groovy app)? The project is Maven based if it matters.

Reason: I'd like to use Spock framework to test my Java app.

Answer

Figured out by myself the steps to make it work:

  1. Add the following plugins to the POM file:

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
            <includes>
                <include>**/*Test.java</include>
                <include>**/*Spec.java</include>
            </includes>
        </configuration>
    </plugin>
    <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.5</version>
        <executions>
            <execution>
                <goals>
                    <goal>addTestSources</goal>
                    <goal>testCompile</goal>
                </goals>
            </execution>
        </executions>
    </plugin>
    
  2. Create src/test/groovy directory

  3. Install GMavenPlus IntelliJ Plugin
  4. If Idea still doesn't see src/test/groovy as a source directory you could delete all the Idea project files from you hard-drive and import your Maven project again

After that CTRL+SHIFT+T will offer you to create new tests either in src/test/java (should be chosen for JUnit tests) or in src/test/groovy (should be chosen for Spock tests).

P.S. command line mvn clean test also works correctly running both JUnit and Spock.