5

Hi i'm quite new to using ant, my instructor wanted us to use it to do some tasks like build the class, run tests..etc

and i am really confused since i cant make the junit work. i have read several other posts and sources but it makes me even more confused.

the error i am getting is:

Testsuite: SolutionTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec

Caused an ERROR
SolutionTest
java.lang.ClassNotFoundException: SolutionTest
  at java.net.URLClassLoader$1.run(URLClassLoader.java:372)
  at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
  at java.security.AccessController.doPrivileged(Native Method)
  at java.net.URLClassLoader.findClass(URLClassLoader.java:360)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
  at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)

this is my build file:

<project name="Solution" basedir="." default="main">
    <property name="src.dir"     value="src"/>
    <property name="build.dir"   value="build"/>

    <property name="classes.dir" value="${build.dir}/classes"/>
    <property name="jar.dir" value="${build.dir}/jar"/>
    <property name="doc.dir" value="${build.dir}/documentation" />
    <property name="test.dir" value="${build.dir}/test" />      

    <target name="compile">
        <mkdir dir="${classes.dir}"/>
            <javac srcdir="${src.dir}" destdir="${classes.dir}" includeantruntime="false">
        </javac>
    </target>

    <target name="jar" depends="compile">
        <mkdir dir="${jar.dir}"/>
            <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
        </jar>
    </target>

    <target name="clean" depends="jar">
        <delete dir="${classes.dir}"/>
    </target>

    <target name="doc" depends="clean">
        <mkdir dir="${doc.dir}" />
        <javadoc destdir="${build.dir}/documentation" sourcefiles="${src.dir}/*.java" />
    </target>

    <target name="junit" depends="doc">
        <mkdir dir="${test.dir}" />
        <junit printsummary="yes" haltonfailure="no">
            <classpath location="." />

            <test name="SolutionTest" haltonfailure="no" todir="${test.dir}" outfile="result">
                <formatter type="plain" />
                <formatter type="xml" />
            </test>
        </junit>
    </target>

    <target name="main" depends="clean, junit"/>
</project>
vsminkov
  • 10,912
  • 2
  • 38
  • 50
Paul
  • 51
  • 1
  • 3
  • JUnit cannot find the tests. Change the `` to point to where you actually place the compiled .class files. I don't see where you're compiling your tests, though. You may need a target similar to the compile target, just with tests.dir or such instead of src.dir. – Robert Sep 13 '16 at 22:33
  • It looks like your junit target doesn't depend on any compile target. It depends on `doc` which depends on `clean`, but there is no compilation step in there. You might need to compile your classes and test dependencies to `test.dir` and then point your `classpath` element to that dir instead of `.` – jmrah Sep 13 '16 at 22:34
  • To make JUnit fail the build when tests fail (or it cannot find them), change `haltonfailure` to "yes" in the junit target. – Robert Sep 13 '16 at 22:34

1 Answers1

7

You should use <junit haltOnFailure="yes" haltOnError="yes" ...> if you want build to fail.

Alternatively you can use failureProperty and errorProperty

<junit failureProperty="test.failure" errorProperty="test.error">
    <!-- your code here -->
</junit>

And verify if those properties was set

<target name="main" depends="clean, junit, verifyNoError, verifyNoFailure"/>

<target name="verifyNoError" if="test.error">
    <fail message="JUnit test or tests errors."/>
</target>

<target name="verifyNoFailure" if="test.failure">
    <echo message="I'm here. Now what?"/>
    <fail message="JUnit test or tests failed."/>
</target>
kris larson
  • 30,387
  • 5
  • 62
  • 74
vsminkov
  • 10,912
  • 2
  • 38
  • 50