Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running groovy unit tests in ant for a java project

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.

like image 671
mfloryan Avatar asked Jan 17 '11 18:01

mfloryan


1 Answers

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.

like image 78
LiorH Avatar answered Oct 27 '22 20:10

LiorH