Skip to content

Using Spock Configuration in Grails

My Suite of Grails functional tests for one of my customers applications have been growing and I have recently added a few tests covering reports that are fairly…ponderous.

So I have decided to tag some of my tests as Slow, so that I can skip them at will, while still running the bulk of my tests.

Segregating one’s tests is a fairly common thing to want and do, with a common solution; see for instance this blog post.

The Grails environment introduces a little wrinkle into the above scheme: as Spock issue 184 so eloquently puts it: “When compiling a SpockConfig file in a Grails project, classes in the project code are not visible. Classes from the project dependencies can be used.”

Issue 184 provides a workaround: use a (class-level in my case, but not absolutely required) annotation that is supplied from a dependency but which has no other effect on the system under test, thus:

import javax.annotation.Resource as Slow

class MySpec extends GebReportingSpec {

It’s worth noting how Groovy’s “import … as …” helps keep the code a bit clearer and should make it easier to munge around if/when issue 184 is fixed.

The remaining issue: how to define whether or not to run Slow tests. Easily done…create the file test/functional/SpockConfig.groovy as shown:

import javax.annotation.Resource as Slow

runner {
    if (['ios.tests.functional.exclude.Slow']) {
        exclude {
            annotation Slow

Since I am a happy IntelliJ user, I set up a run configuration that looks like:

Of course, this same command line can be used away from Intellij if needed.

I’m now a happy and more productive tester.

Tags: , , , ,

C, Java Enterprise Edition, JEE, J2EE, JBoss, Application Server, Glassfish, JavaServer Pages, JSP, Tag Libraries, Servlets, Enterprise Java Beans, EJB, Java Messaging Service JMS, BEA Weblogic, JBoss, Application Servers, Spring Framework, Groovy, Grails, Griffon, GPars, GAnt, Spock, Gradle, Seam, Open Source, Service Oriented Architectures, SOA, Java 2 Standard Edition, J2SE, Eclipse, Intellij, Oracle Service Bus, OSB