Kohei Nozaki's blog 

Testing build.xml itself with AntUnit


Posted on Wednesday Jan 28, 2015 at 03:27PM in Technology


In my prevous entry I used AntUnit as a custom task unit testing framework. this time I’d try it for testing build.xml file itself (no custom tasks). it enables with antcallback task which is part of ant-contrib.

Test target is following. simply loads a properties file, then set a property named result which will be asserted in the caller (test case).

<project name="mybuild" basedir=".">

    <target name="main">
        <loadproperties srcFile="mybuild.properties"/>
        <property name="result" value="Hello from mybuild | ${file.var}"/>
    </target>

</project>

Test case is fairly simple as follows. it imports the test target and invoke a target with antcallback, then acquire a property in the calling target. then assert whether the property is set as expected. additionally, it puts a parameter named basedir to enable loading of mybuild.properties which is placed in the same directory to mybuild.xml.

<project name="mybuildTest" xmlns:au="antlib:org.apache.ant.antunit" xmlns="antlib:org.apache.tools.ant">

    <taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

    <import file="${project.build.outputDirectory}/mybuild.xml"/>

    <target name="setUp">
        <echo>setup</echo>
    </target>

    <target name="test1">
        <antcallback target="mybuild.main" return="result">
            <!-- intended to enable loading mybuild.properties -->
            <param name="basedir" value="${project.build.outputDirectory}"/>
        </antcallback>

        <au:assertTrue>
            <equals arg1="Hello from mybuild | Hello from mybuild.properties" arg2="${result}"/>
        </au:assertTrue>
    </target>

    <target name="tearDown">
        <echo>tearDown</echo>
    </target>
</project>

Entire the project is available in my GitHub repository. you can test it on your environment easily with mvn clean test because Maven collects all of dependencies.

References: