Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499

Unit Testing Using Groovy, JUnit, and Maven

One of my clients had to use Groovy scripts to synchronize thousands of user identities between multiple data sources. When I started working on the project the development process looked similar to this:


It’s obvious that this process was far from being efficient. It was very time consuming and, most importantly, errors were caught too late in the development cycle making them very expensive to fix.

Over the years, I’ve learned to ask myself the following question over and over again: what can I do to make this process more efficient? The answer in this case was simple:

Test earlier. Test better.

So I decided to introduce a new testing phase earlier in the process:


I had to tackle some problems along the way so I decided to write a blog post about it.


For this example we will be using the following components:

The source code for the sample maven project is available on github.

Project Dependencies

First, you need to add the following dependencies to your pom.xml:

Then, we need to instruct maven to compile our Groovy source files (if any) and run the JUnit test cases during the build process. The only thing we need to do is add the gmaven-plugin to the pom.xml as follows:

By default, the plugin expects all the Groovy source files to be located under “src/main/groovy” and the Groovy test files under “src/test/groovy”.

Sample Project

Our sample application consists of a single Groovy source file named “MyGroovyClass.groovy”. This file contains the class (MyGroovyClass) that we want to test.


I decided to add two different flavors of the same test, one for each JUnit version, to clarify the differences between the two. When using JUnit 3 you have to extend GroovyTestCase which, besides telling Groovy to run the class as a test, it also offers additional functionality, such as assertion methods, that will come in handy when writing your tests. Here are the contents of MyGroovyClassJUnit3Test.groovy:

Take a look at the GroovyTestCase API docs for a detailed explanation of all the assertion methods.

If you decide to use JUnit 4, you can directly use standard JUnit annotations (@Before, @After, @Test, etc…). However, you’ll have to import GroovyAssert to use assertion methods in your tests:

Another thing to notice is that JUnit 4 lets you use the @BeforeClass and @AfterClass annotations so that you can execute some code before and after all the tests in the class have been run (this feature is not supported in JUnit 3). This is different than using the @Before and @After annotations (or the equivalent setUp() and tearDown() methods in JUnit 3). The methods annotated with @Before and @After will execute after each test is run.

Run the Test Cases

To execute all the test cases run the following maven command:

You should see an output similar to this:

You can also run individual test cases by passing the test name as a maven property:

Final Notes

Regular Java classes can also be tested from your Groovy test files. Just place you classes in “src/main/java” as usual and import them in your Groovy test classes. Take a look at the following examples for more details about how to test Java classes: MyJavaClassJunit3Test.groovy and MyJavaClassJunit4Test.groovy.

The GroovyAssert class implementation was first added to Groovy 2.3. So if you are using Groovy 2.2 or older you will have to import org.junit.Assert directly to obtain similar results.

Warning: A non-numeric value encountered in /home/guillerm/public_html/wp-content/themes/Builder-Cohen/lib/builder-core/lib/layout-engine/modules/class-layout-module.php on line 499