Tag Archives: maven

Running groovy junit tests with Surefire in a java/maven project

4 Apr

I wanted to start using Groovy for some automated tests that I needed to write because in my research I found that it had support for a lightweight SOAP Client (Groovy-wslite, btw) which wasn’t as code-heavy as an equivalent java implementation. Having never coded anything in Groovy before, I thought I would slowly inch into it and start off with a java/maven project with groovy unit tests.

All ran perfectly in my IDE, but when it came time to start running it via a maven build using surefire to run my junit tests, I noticed that my groovy tests weren’t running.

There were a couple of things that were stopping my tests from running, and thought I would share to maybe save someone some time trying to figure it out.

1. Use the gmaven-plugin to allow maven to compile Groovy code

This is as simple as adding a plugin in your pom:

<!-- Groovy compilation -->
 <plugin>
   <groupId>org.codehaus.gmaven</groupId>
   <artifactId>gmaven-plugin</artifactId>
   <version>1.5</version>
   <executions>
     <execution>
       <goals>
         <goal>compile</goal>
         <goal>testCompile</goal>
       </goals>
     </execution>
   </executions>
 </plugin>

2. Make sure your Groovy test classes live in src/test/groovy

This one’s pretty self explanatory… Make sure you put your groovy test classes within the src/test/groovy directory in your project.

3. Ensure you’ve configured surefire to look for test files other than ones that end with “.java”

Update the “includes” configuration to ensure it won’t only identify test classes ending with “.java”.

<!-- Running tests -->
 <plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>2.12.3</version>
   <configuration>
     <failIfNoTests>true</failIfNoTests>
     <includes>
       <include>**/*Test*.*</include>
     </includes>
   </configuration>
 </plugin>