Using Groovy Spock in a Maven Java Project

Groovy Spock is a testing framework that can be used as an alternative to the venerable JUnit. In Java projects it’s probably very common (I don’t have any data, just an assumption based on how I think) to also use a Java based testing framework. The most widely known is JUnit, although not the only one of its kind (e.g. see this article on DZone). However, Java’s syntax can sometimes be rather cumbersome and verbose, and this is where a dynamic language like Groovy can help. It is often used to create nice and interesting DSLs, e.g. as the basis of the Gradle project or, as in the case of Spock, for testing.

Here’s how to integrate the Groovy Spock testing framework in a Maven based Java project.

One thing up front: I’m no fan of Groovy. I’ve worked with Grails projects for several years and using Groovy has more than once proven to be a problem. Especially in very large applications. However, I do see the benefits it can provide in certain situations and I have come to like the more expressive, although sometimes odd to read, Spock DSL in tests.

I won’t describe all the intricacies of Spock in this article. Please make use of the documentation and StackOverflow for details. What I will do is describe how Spock can be incorporated into a Java project that uses Maven as a build tool.

Therefore, I have created a simple Java project on GitHub. It verifies the correctness of the Deutsche Bahn train numbers based on a simple algorithm (German explanation on Wikipedia). I’m using only 8 numbers and a control code instead of 11 and a control code which is how I initially came upon this exercise. Anyway, since that’s not relevant for the actual topic we’ll just ignore this fact.

From years of using JUnit in your project you can probably tell anybody who asks how to add it to the pom.xml – at 3 AM, drunk. So, it’s safe to assume Spock works similarly. And it does, in part. First, add the dependency (exchange version numbers if new ones have been released since the writing of this article). It does also work alongside JUnit, as my example shows.

<dependency>
    <groupId>org.spockframework</groupId>
    <artifactId>spock-core</artifactId>
    <version>1.2-groovy-2.5</version>
    <scope>test</scope>
</dependency>

That won’t be enough though. Maven doesn’t know how to handle Groovy files, which is where GMavenPlus comes in. Before we add it to the pom.xml though, let’s try and run the tests first and see what happens.

JavaMavenSpockProblem

As you can see, I’m working on Windows and you’ll probably judge me for that. But that’s not the point.

There’s no error and seven tests have actually been run. But you should know from looking at the Github repo that the project contains seven JUnit and seven Spock tests. What you can see here is the result of the JUnit test having been executed. No error, but also no Spock tests.

Now add this little snippet (remember to check version numbers for new releases).

<plugin>
    <groupId>org.codehaus.gmavenplus</groupId>
    <artifactId>gmavenplus-plugin</artifactId>
    <version>1.6.2</version>
    <executions>
        <execution>
            <goals>
                <goal>compileTests</goal>
            </goals>
        </execution>
    </executions>
</plugin>

And here’s the resulting output.

JavaMavenSpockSuccess

Aside from those WARNING message that scream for attention like a little princess, the correct number of executed tests is shown, 14. It’s only logical that Spock is now working.

(I’ll take another look at the warning messages and may update the article accordingly)

One thought on “Using Groovy Spock in a Maven Java Project

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.