Versions Compared


  • This line was added.
  • This line was removed.
  • Formatting was changed.

Groovy has excellent support for Testing with Unit Testing (GroovyTestCase adds extra capabilities to TestCase from JUnit 3.8.2) and Mocking capabilities built right in. Currently, there are no special Groovy extensions for JUnit 4 but it's easy to use so long as you are using Groovy 1.1 (currently in beta) 5 and Java 5+ (a requirement for annotations/Junit 4.x). Here are some examples.

Make sure you are using Groovy 1.1 5 (we used beta-21.5.6) and JUnit 4.x (we used 4.4).


Code Block
import org.junit.runner.*
import org.junit.experimental.theories.*
import static org.junit.Assume.assumeTrue as assume

class LanguageTheoryTest {
    @DataPoint public static String java = 'Java'
    @DataPoint public static String ruby = 'JRuby'
    @DataPoint public static String python = 'Jython'
    @DataPoint public static String javascript = 'Rhino'
    @DataPoint public static String groovy = 'Groovy'
    @DataPoint public static String scala = 'Scala'
    @DataPoint public static String csharp = 'C#'

    def jvmLanguages = [java, ruby, python, groovy, scala, javascript]

    def teamSkills = [
        tom:   [java, groovy, ruby],
        dick:  [csharp, scala, java, python],

        harry: [javascript, groovy, java]

    @Theory void everyone_knows_java() {
        teamSkills.each { person, skills ->
            assert java in skills

    @Theory void someone_knows_each_jvm_language(String language) {
        assume language in jvmLanguages
        assert teamSkills.any { person, skills ->
            language in skills

    @Theory void tom_knows_all_languages_ending_with_y(String language) {
        assume language.endsWith('y')
        assert language in teamSkills.tom