13

I have a Java project with some unit tests written using JUnit. Recently some new unit tests have been added that are written in groovy (also using JUnit) as it's easier to make those more expressive and generally easier to read. It also allows us to use the spock framework.

The project is build and tested with ant.

Before the groovy classes were added unit tests were run using the following ant task:

<target name="test" depends="test-compile">
    <junit printsummary="yes">
        <classpath>
            <path refid="test.classpath"/>
        </classpath>
        <formatter type="plain"/>
        <batchtest fork="yes" todir="${test.dir}/report">
            <fileset dir="${test.dir}/unit" includes="**/*.java"/>
        </batchtest>
    </junit>
</target>

However, this approach does not work for groovy tests as those are in *.groovy files and the JUnit Ant task, understandably, does not recognise them in the fileset.

The alternative approach is to use *.class files for the batchtest fileset like this:

<batchtest fork="yes" todir="${test.dir}/report">
    <fileset dir="${test.dir}/${build.dir}">
        <include name="**/*Test*.class" />
    </fileset>
</batchtest>

This generates false negatives as closure class files are also included so a possible workaround is to exclude those files.

<batchtest fork="yes" todir="${test.dir}/report">
    <fileset dir="${test.dir}/${build.dir}">
        <include name="**/*Test*.class" />
        <exclude name="**/*$*.class" />
    </fileset>
</batchtest>

Is there a better way to identify test classes fo the junit ant task? Perhaps one based on reflection and the @Test attribute as manually listing all the test classes (which would work perfectly well) is not really a maintainable solution. Something like the SpecClassFileSelector from the Spock framework.

3 Answers 3

3

what about changing the include pattern to *Test rather than *Test* as @jon-skeet suggested here.

This way it will not match the anonymous closure classes.

you'll have to rename your existing classes and ask the developers to follow this pattern.

0

Take a look at:

http://www.ibm.com/developerworks/java/library/j-pg11094/

There's a groovyc ant taskdef for compiling the groovy test cases and running them. The example there is Maven, but it shouldn't be too hard to adapt it to do what you want.

1
  • Thanks. I have seen this page before writing the question. My two proposed solutions actually do what the maven solution does - look at the classpath.
    – mfloryan
    Commented Jan 21, 2011 at 8:45
-1

Can't you just write something like this?

        <fileset dir="${test.dir}/unit" includes="**/*.java,**/*.groovy"/>
2
  • 1
    No, because it does not work. According to the JUnit ant task documentation: batchtest collects the included resources from any number of nested Resource Collections. It then generates a test class name for each resource that ends in .java or .class
    – mfloryan
    Commented Jan 22, 2011 at 11:37
  • 1
    I've been using Gradle lately. You should really take a look a it, it's so much easier and flexible than Ant, and especially than Maven. The Groovy plugin lets you build projects with Java and Groovy classes, as well as test using Groovy and Java.
    – Chochos
    Commented Jun 21, 2011 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.