Running Groovy tests in Java

As I’ve mentioned before, JUnit tests written in Groovy (against a Java application) are sometimes easier to write than in plain-Jane Java because of Groovy’s relaxed syntax. And fewer imports, fewer type declarations, and no semi-colons are just the tip of the iceberg too, man.

Running Groovy JUnit tests through an existing build process managed by Ant or Maven is simple too- especially when a project already has existing hip Java JUnit tests. Ideally, in this scenario, the difference between a Java test and a Groovy one should be seamless- having two different tasks that run each platform’s tests seems like overkill- why not run the Groovy ones with the Java ones?

Groovy bundles a TestSuite type dubbed AllTestSuite, which facilitates running Groovy JUnit scripts from within an IDE and of course, a build process that already runs normal Java JUnit tests. The copasetic class works by looking for System properties which delineate where Groovy tests reside and a naming pattern (such as tests/groovy and **/*Test.groovy).

What I find most effective; however, is to create my own dynomite TestSuite type, set the System properties in a static block and then override the suite method and call AllTestSuite’s suite like so:

package test.com.acme.sda;

import groovy.util.AllTestSuite;
import junit.framework.Test;
import junit.framework.TestSuite;

public class AllGroovySuite extends TestSuite {	

 static {
  System.setProperty(AllTestSuite.SYSPROP_TEST_DIR, "./test/groovy");
  System.setProperty(AllTestSuite.SYSPROP_TEST_PATTERN, "**/*Test.groovy");
 }

 public static void main(String[] args) {
  junit.textui.TestRunner.run(AllTestSuite.suite());
 }	

 public static Test suite() {
  return AllTestSuite.suite();
 }
}

Note how AllGroovySuite is written in normal Java and provides values for Groovy’s AllTestSuite‘s SYSPROP_TEST_DIR and SYSPROP_TEST_PATTERN. When this class is run via a JUnit runner, the SYSPROP_TEST_DIR directory is scanned and any file matching the SYSPROP_TEST_PATTERN pattern is loaded by AllTestSuite and run.

By adopting this technique, I can run Groovy test scripts in an IDE, like Eclipse (and of course, see the green bar); furthermore, existing test tasks in Ant or goals in Maven can pick this class up and run it (which then runs all Groovy tests). Pretty groovy, eh?

Post to Twitter

Related odds and ends
 

One Response to “Running Groovy tests in Java”


  1. [...] I recently came across Scott Hickey’s article in IBM DeveloperWorks comparing writing code in Java and Groovy. Simply code written in Groovy has simplified syntax comparing to what we are used to. Hence is less noisy, easier to read and understand. Isn’t it great to focus on solving a problem than on typing curly brackets all the time? What I just realized is that we are boxed by our behaviour, we are get actually used to type the brackets, for loops with index, iterator. Type, type, type… And we think that’s the way software development should look like. Thank’s to good IDEs helping us to generate that noise we are not frustrated completely yet. In Java 5 we have got already some goodies like simplified loops, autoboxing, generics. That is a good change I believe. And I don’t care that generics can have bad influence on performance (see an article on TSS). Coming back to Groovy I will try to have a look how can I incorporate it to my current development behaviour. I’ve read an article about using Groovy for unit testing of java code. Sounds promising to me. [...]