tags:

views:

51

answers:

1

Hi, I've got a simple test case in /build directory that I'm trying to compile and run with Ant. Compilation is fine, but I can't figure out how to run this. The class is:

package money;

import org.junit.*;
import static org.junit.Assert.*;

public class MoneyTest {

    @Test
    public void testAmount() {
        Money m = new Money(20);
        System.out.println("AMOUNT: " + m.amount());
        System.out.println("DOUBLE AMOUNT: " + m.doubleAmount());
        assertEquals(21, m.amount());
    }

}

and the buildfile goes like this:

<?xml version="1.0"?>
<project name="Money" default="build-source" basedir=".">

    <description>The Money project build file.</description>
    <property name="src" location="."/>
    <property name="build" location="build"/>
    <property name="junit" location="lib/junit-4.8.2.jar"/>

    <path id="_classpath">
        <pathelement path="${junit}"/>
        <pathelement path="${build}"/>
    </path>

    <target name="prepare">
        <mkdir dir="${build}"/>
    </target>

    <target name="build-source" depends="prepare" description="compile the source ">
        <javac srcdir="${src}" destdir="${build}">
            <classpath refid="_classpath"/>
        </javac>
    </target>

    <target name="run" depends="build-source">
        <java classname="${build}/MoneyTest">
            <classpath refid="_classpath"/>
        </java>
    </target>

</project>

when I run "ant run" from the Terminal, it says that it cannot find the class at that path.

Thanks beforehand!

+2  A: 

You've specified a location as a classname. The name of the class would be money.MoneyTest, not /home/build/money/MoneyTest.

However, you should be using the junit task instead of the java task anyway, given that it's a JUnit test rather than a Java app itself. (It doesn't have a main method, for example.)

Jon Skeet
Jon,-thanks for the feedback. I've changed it to: <target name="run" depends="build-source"> <junit dir="money.MoneyTest"> <classpath refid="_classpath"/> </junit> </target>and it builds now. Is "dir" the right attribute to use with the "junit" task here? (I can see no test output, but that's probably because I need to declare a separate task for that).
rubyist
@rubyist: No, `dir` specifies a directory. Use the `test` element instead. Look at the bottom of the linked docs page for examples.
Jon Skeet
I've made it work now. Thanks very much for your help.
rubyist