A lot of people nowadays wonder if there might be a field of application for alternative programming languages in their Java applications. A good and relatively safe approach is to allow the usage of other languages for unit tests. In fact, when it comes to testing there is really no reason to bother with Java’s verbosity and strictness.

A good candidate in this field is Groovy, because it is very simple to use, both for Java developers and non programmers (e.g. the testers in your team). Also, the interoperability with Java is excellent and Groovy is easy to integrate into an existing Maven build process.

Just create a new folder src/test/groovy in your Maven project and add the following to pom.xml:

<dependencies>
  <dependency>
    <groupId>org.codehaus.groovy</groupId>
    <artifactId>groovy-all</artifactId>
    <version>2.1.1</version>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <plugins>
    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>build-helper-maven-plugin</artifactId>
      <version>1.7</version>
      <executions>
        <execution>
          <id>add-source</id>
          <phase>generate-test-sources</phase>
          <goals>
            <goal>add-test-source</goal>
          </goals>
          <configuration>
            <sources>
              <source>src/test/groovy</source>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
    <plugin>
      <groupId>org.codehaus.gmaven</groupId>
      <artifactId>gmaven-plugin</artifactId>
      <version>1.5</version>
      <executions>
        <execution>
          <goals>
            <goal>testCompile</goal>
          </goals>
          <configuration>
            <sources>
              <fileset>
                <directory>${pom.basedir}/src/test/groovy</directory>
                <includes>
                  <include>**/*.groovy</include>
                </includes>
              </fileset>
            </sources>
          </configuration>
        </execution>
      </executions>
    </plugin>
</plugins>

If you’re using Eclipse you might also want to install the Eclipse Groovy IDE: http://groovy.codehaus.org/Eclipse+Plugin

And now you’re ready to start: Create a file src/test/groovy/Test.groovy:

import org.junit.Test
import static org.junit.Assert.*

class Test {
  @Test void firstTest() {
    def str = "test" + 1
    def bd = 22.3 * 3
    assertNotNull str
    assertEquals "test1", str
    assertTrue str == 'test1'
    assertEquals 66.9, bd
    assertTrue bd.class == BigDecimal
  }
}

And start the test case as usual in your favourite IDE or with the Maven test goal. And that’s basically it.

One of the interesting features of Groovy is, that you can omit semicolons and parentheses, which makes JUnit appear like a neat internal DSL. Other Groovy features handy for testing are:

  • The possibility to compare string with ==
  • Simplified handling of BigDecimal’s
  • Support for closures, which can be used to test multiple values in one step:
    [0, 3, 5, 7, 9, 10].each{ 
      assertNotNull classUnderTest.calculate(it)) 
    }
    
  • Simplified testing of collection content. For example this assertion checks if a person with name “Foo” exists in the collection:
    def persons = //Query for a list of persons
        
    assertTrue "Foo" in person*.name
    

Leave a Reply

Your email address will not be published. Required fields are marked *


*