Kengo's blog

Technical articles about original projects, JVM, Static Analysis and TypeScript.

JUnit with Groovy2 by Maven3

Groovy is a good language to code JUnit test cases. Groovy is easy to learn, short to code, and simple to read. Today I changed my pom.xml to use Groovy 2.0 with JUnit 4.11, so I will share my change for you.

how to edit your pom.xml

What you have to add is not only gmaven-plugin, but also gmaven.runtime property to specify version of Groovy. Of course you have to add a dependency on Groovy itself, you can use test scope so it does not affect your package.

Your Groovy script should be put into src/test/groovy. Do not forget to use 'Test' as suffix when name your classes, because surefire-plugin needs it.

<project>
  <properties>
    <gmaven.runtime>2.0</gmaven.runtime>
  </properties>
  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.gmaven</groupId>
        <artifactId>gmaven-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <id>default-testCompile</id>
            <phase>test-compile</phase>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>
  <dependencies>
    <dependency>
      <groupId>org.codehaus.groovy</groupId>
      <artifactId>groovy-all</artifactId>
      <version>2.0.6</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>