麻烦 运行 通过 Ant 在 eclipse 上进行简单的 JUnit 测试
Trouble Running simple JUnit Test through Ant on eclipse
我在 eclipse 中进行了以下 JUnit 测试:
package test;
import org.junit.Test;
public class SimpleJUnitTest
{
@Test
public void doTest() { System.out.println("Test did run"); }
}
和以下 build.xml 在同一文件夹中:
<?xml version="1.0" encoding="UTF-8"?>
<project name="LoggerTest" default="JUnitTest" basedir=".">
<target name="JUnitTest">
<junit>
<classpath location="../../lib/junit.jar" />
<test name="test.SimpleJUnitTest" />
</junit>
<echo>boo</echo>
</target>
</project>
如果我 运行 在 "Run As..." 下测试 class 并选择 JUnit,它 运行 没有错误。如果我在 "Run As..." 下 运行 build.xml 并选择 Ant Build,我会得到以下输出:
Buildfile: C:\Users5868\workspace\JUnit1\tst\test\build.xml
JUnitTest:
[junit] Test test.SimpleJUnitTest FAILED
[echo] boo BUILD SUCCESSFUL Total time: 390 milliseconds
如果我删除 JUnit 下的 classpath 属性,我会收到一条关于需要 classpath 上的 junit jar 的不同错误消息,因此我认为 JUnit 正在被调用。我只是不明白这里的错误是什么。我尝试将静态块代码放在 class 中以在加载 class 时执行 System.out.println()
,但它没有出现,所以我似乎做错了什么在配置中。
谁能告诉我这里出了什么问题?
编辑:
directory structure:
JUnit1
--bin
--test
--SimpleJUnitTest
--lib
--junit.jar
--scripts
--build.xml
--src
--tst
--test
--SimpleJUnitTest.java
我也将 build.xml 复制到 tst 并从该目录的命令行复制 运行 它,结果相同。
我已经将 junit.jar 复制到 %ant_home%\lib 但没有任何效果,尽管当我从 classpath 中取出 pathelement 行时我收到消息 "The for must include junit.jar if not in Ant's own classpath"。我不确定 "Ant's own classpath" 在哪里指定。带有新错误消息的 class 路径块是这样的:
<classpath>
<pathelement location="c:/users/995868/apache-ant-1.9.4/lib" />
<pathelement location="../bin" />
</classpath>
我没有在任何地方使用 hamcrest 功能,所以我没有查找并放入它。我试图做一个简单的例子,而 ant 下的 junit 的文档(至少)没有提到那个 hamcrest 是必要的。
您的 class 不包含任何测试用例,这是一个错误。您的@Test 注释将被忽略,因为您没有指定将实际使用它的@RunWith,因此JUnit 搜索名为"test..." 的方法。
所以你有两个选择:
a) 将您的方法重命名为 "testSomething" 或类似名称。
b) 在您的 class 定义
上方添加 @RunWith(BlockJUnit4ClassRunner.class)
这两种方式都将确保您的 class 中至少存在一个测试,无论是通过命名约定 (a) 还是通过注释 (b)。
我想你从你的蚂蚁class路径中遗漏了hamcrest-core.jar。
编辑: 并且您的 class 路径中也缺少测试文件 class 本身。因此,您需要按以下方式更新 classpath:
<classpath>
<pathelement location="bin"/> <!--here is your test class-->
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="lib/junit-4.12.jar"/>
</classpath>
我在 eclipse 中进行了以下 JUnit 测试:
package test;
import org.junit.Test;
public class SimpleJUnitTest
{
@Test
public void doTest() { System.out.println("Test did run"); }
}
和以下 build.xml 在同一文件夹中:
<?xml version="1.0" encoding="UTF-8"?>
<project name="LoggerTest" default="JUnitTest" basedir=".">
<target name="JUnitTest">
<junit>
<classpath location="../../lib/junit.jar" />
<test name="test.SimpleJUnitTest" />
</junit>
<echo>boo</echo>
</target>
</project>
如果我 运行 在 "Run As..." 下测试 class 并选择 JUnit,它 运行 没有错误。如果我在 "Run As..." 下 运行 build.xml 并选择 Ant Build,我会得到以下输出:
Buildfile: C:\Users5868\workspace\JUnit1\tst\test\build.xml
JUnitTest:
[junit] Test test.SimpleJUnitTest FAILED
[echo] boo BUILD SUCCESSFUL Total time: 390 milliseconds
如果我删除 JUnit 下的 classpath 属性,我会收到一条关于需要 classpath 上的 junit jar 的不同错误消息,因此我认为 JUnit 正在被调用。我只是不明白这里的错误是什么。我尝试将静态块代码放在 class 中以在加载 class 时执行 System.out.println()
,但它没有出现,所以我似乎做错了什么在配置中。
谁能告诉我这里出了什么问题?
编辑:
directory structure:
JUnit1
--bin
--test
--SimpleJUnitTest
--lib
--junit.jar
--scripts
--build.xml
--src
--tst
--test
--SimpleJUnitTest.java
我也将 build.xml 复制到 tst 并从该目录的命令行复制 运行 它,结果相同。
我已经将 junit.jar 复制到 %ant_home%\lib 但没有任何效果,尽管当我从 classpath 中取出 pathelement 行时我收到消息 "The for must include junit.jar if not in Ant's own classpath"。我不确定 "Ant's own classpath" 在哪里指定。带有新错误消息的 class 路径块是这样的:
<classpath>
<pathelement location="c:/users/995868/apache-ant-1.9.4/lib" />
<pathelement location="../bin" />
</classpath>
我没有在任何地方使用 hamcrest 功能,所以我没有查找并放入它。我试图做一个简单的例子,而 ant 下的 junit 的文档(至少)没有提到那个 hamcrest 是必要的。
您的 class 不包含任何测试用例,这是一个错误。您的@Test 注释将被忽略,因为您没有指定将实际使用它的@RunWith,因此JUnit 搜索名为"test..." 的方法。
所以你有两个选择:
a) 将您的方法重命名为 "testSomething" 或类似名称。
b) 在您的 class 定义
上方添加 @RunWith(BlockJUnit4ClassRunner.class)这两种方式都将确保您的 class 中至少存在一个测试,无论是通过命名约定 (a) 还是通过注释 (b)。
我想你从你的蚂蚁class路径中遗漏了hamcrest-core.jar。
编辑: 并且您的 class 路径中也缺少测试文件 class 本身。因此,您需要按以下方式更新 classpath:
<classpath>
<pathelement location="bin"/> <!--here is your test class-->
<pathelement location="lib/hamcrest-core-1.3.jar"/>
<pathelement location="lib/junit-4.12.jar"/>
</classpath>