作为 Ant 目标运行 JUnit 测试
我在 /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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已将位置指定为类名。该类的名称为
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 thejava
task anyway, given that it's a JUnit test rather than a Java app itself. (It doesn't have amain
method, for example.)