作为 Ant 目标运行 JUnit 测试

发布于 2024-09-14 12:00:29 字数 1562 浏览 3 评论 0原文

我在 /build 目录中有一个简单的测试用例,我正在尝试使用 Ant 对其进行编译和运行。编译没问题,但我不知道如何运行它。该类是:

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());
    }

}

并且构建文件如下所示:

<?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>

当我从终端运行“ant run”时,它说它无法在该路径中找到该类。

预先感谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

祁梦 2024-09-21 12:00:29

您已将位置指定为类名。该类的名称为 money.MoneyTest,而不是 /home/build/money/MoneyTest

但是,您应该使用 junit 任务无论如何,而不是 java 任务,因为它是 JUnit 测试而不是 Java 应用程序本身。 (例如,它没有 main 方法。)

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文