Home > Groovy > Groovy + JUnit + Ant

Groovy + JUnit + Ant

I found it extremely difficult over the past couple days to get Groovy and JUnit and Ant to play nicely together. If you have the opportunity to use Maven for your builds, do it. If you’re stuck with Ant, read on.

I’m writing unit tests in Groovy for an application that’s written entirely in Java. That part is easy – Groovy and Java work well together. And since Groovy ignores scope, you can access the private methods you wrote in Java (or Groovy), which makes testing even easier. Rather than using Reflection or calling the public methods that call those private methods, you can call your private methods directly from a Groovy class. Groovy treats them as if they’re public.

To use Groovy + JUnit + Ant, first you need to compile your Groovy files. This part is easy, if you know you have to do it. The groovyc Ant Task has some great examples you can copy and tweak, but make sure you put the groovy-all-VERSION.jar on your classpath. The similarly-named jar without the word “all” in it won’t work.

The hard part about Groovy + JUnit + Ant is this:

“By default Groovy unit test cases generate Java bytecode and so are just the same as any other Java unit test cases. One thing to watch is often Ant / Maven look for *.java files to find unit tests with pattern matching, rather than *.class files.” – Unit Testing (emphasis mine)

Here’s an example from the Ant JUnit Task page:

  <!-- some details omitted -->
    <fileset dir="${src.tests}">
      <include name="**/*Test*.java"/>
      <exclude name="**/AllTests.java"/>

This doesn’t work for Groovy tests, but it should be easy enough to fix – just remove the “.java” extensions, right? Wrong. The JUnit task can find *.java files in your source directory, but not *.groovy files. So you need to change the fileset to point to your classes directory in addition to removing the “.java” suffix from the include and exclude. And if you use closures in your Groovy tests, they’ll get compiled to inner classes in separate files, so you probably want to exclude those from your fileset. Here’s what my task looked like when I finished:

  <!-- some details omitted -->
    <fileset dir="${classes}">
      <include name="**/*Test*"/>
      <!-- a '$' in the filename means it's an inner class -->
      <exclude name="**/*$*"/>

Make sure to add some reporting and code coverage so you can see the statistics from your tests. Happy Anting.

Tags: , ,
  1. James
    October 28, 2008 at 7:18 am

    thanks, I’ve used this tip to introduce groovy into our tests


  2. November 8, 2008 at 4:46 pm

    Glad it helped!

  3. October 28, 2009 at 2:35 pm

    Thanks — I just added some Groovy tests and this helped a lot.

  4. October 28, 2009 at 8:44 pm

    @Kevin – glad you found this to be helpful. I’ve lately been doing my Groovy and JUnit with Maven and have been very happy with it.

  5. Allan
    August 16, 2010 at 11:59 am

    I just ran into this. Everything worked, groovy was compiling, the IDE invocations of test cases worked, but I got no coverage.

  1. December 31, 2009 at 3:17 pm

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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: