简体   繁体   中英

Running an JUnit test as Ant target

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!

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM