Unit testing with Groovy

The Groovy language can be used to make unit testing for Java projects more convenient. This article shows how to integrate Groovy unit testing with Maven builds.

Maven integration

Groovy can be integrated with Maven Builds using GMaven, Mat Schaffers Blog and the usual blob of XML configuration:

<!-- ... -->
<dependencies>
    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>1.7.3</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.gmock</groupId>
        <artifactId>gmock</artifactId>
        <version>0.8.0</version>
    </dependency>
</dependencies>
<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.groovy.maven</groupId>
            <artifactId>gmaven-plugin</artifactId>
            <version>1.0</version>
            <extensions>true</extensions>
            <executions>
                <execution>
                    <goals>
                        <goal>testCompile</goal>
                    </goals>
                    <!-- allow .groovy scripts in src/test/java in addition to src/test/groovy -->
                    <configuration>
                        <sources>
                            <fileset>
                                <directory>${pom.basedir}/src/test/java</directory>
                                <includes>
                                    <include>**/*.groovy</include>
                                </includes>
                            </fileset>
                        </sources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>

Update: As Peter pointed out, this configuration will not use the latest Groovy version to compile. See GMAVEN-62 Simpler GMaven POM configuration.

Eclipse integration

Groovy can be integrated into the Eclipse IDE using the Groovy Eclipse plug-in. You need to install the Groovy-Eclipse Feature from the software site:

I can highly recommend the Preferences > Groovy > Editor > Copy Java Color Preferences button.

You can run Groovy test cases as usual with Run As > JUnit.

JUnit integration

Groovy comes with a GroovyTestCase base class for tests which gets most of the attention in the Groovy testing documentation. Unfortunately, this is based on the outdated JUnit version 3.8, so I don’t want to use this. Fortunately, you can also use plain JUnit 4 with Groovy.

Advantages

Great about Groovy is that you can rename .java classes to .groovy and expect to be able to run them, as Groovy is compatible to Java (you will stumble on minor distinctions along the way). For testing, the following syntax sugar is particular helpful:

String expressions:

assertEquals("Name: ${p.name}", value)

Properties instead of getters and setters, obj.with { ... } and collection literals to create test data:

(person = new Person("Otto", "Mustermann")).with {
    it.id = 1
    it.street = "17 Rue Charlot"
    it.city = "Paris"
    it.knownTo = [p2, p3]
}

Meaningful test names:

@Test void "call handled"() {
    // ...
}

Mocking: mock(Class) { ... } and play { ... } with GMock:

@WithGMock
public class PersonServiceTest {

    @Test void "call handled"() {
    (personService = new PersonService()).with {

        it.personRepository = mock(PersonRepository) {
            get(1).returns(person).stub()
            list().returns([person]).stub()
        }

        it.notificationService = mock(NotificationService) {
            // ...
        }
    }

    // in the 'play'-block mocks can be used and are verified afterwards
    play {
        personService.handleCall(person)
    }

}
Ralf Ebert

Ralf Ebert is an independent software developer, technical writer and trainer. He makes apps for Mac OS X and iOS and builds software solutions for companies using Eclipse RCP and Ruby on Rails. He offers training courses for software developers and writes books and articles about software development.