使用ant做junit测试时,出现java.lang.NoClassDefFoundError: junit/framework/TestResult错误
我的代码如下结构如下:待测试类:
package org.juns.junit;
public class HelloWorld {
public String hello(){
return "hello";
}
public String world(){
return "world";
}
public String nil(){
return null;
}
public String notNil(){
return "abc";
}
public String ext(){
throw new NumberFormatException();
//return null;
}
}
测试类:
package org.juns.junit.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.juns.junit.HelloWorld;
import static org.junit.Assert.*;
public class TestHello{
private HelloWorld hw;
@Before
public void setUp() throws Exception {
hw=new HelloWorld();
}
@Test
public void testHello(){
String str=hw.hello();
assertEquals("测试失败", str, "hello");
}
@Test
public void testworld(){
String str=hw.world();
assertEquals("测试失败", str, "world");
}
@Test
public void testnil(){
assertNull("对象不为空",hw.nil());
}
@Test
public void testnotNil(){
assertNotNull("对象为空", hw.notNil());
}
@Test(expected=NumberFormatException.class)
public void testext(){
hw.ext();
}
@After
public void tearDown() throws Exception {
hw=null;
}
}
build.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<project name="junit-test" default="end">
<property name="src.dir" location="src"></property>
<property name="lib.dir" location="lib"></property>
<property name="test.src.dir" location="test"></property>
<property name="build.dir" location="build"></property>
<property name="build.classes" location="${build.dir}/classes"></property>
<property name="build.test.dir" location="${build.dir}/test"></property>
<property name="build.test.classes" location="${build.test.dir}/classes"></property>
<property name="build.test.report" location="${build.test.dir}/report"></property>
<property name="run.test.class" value="org.juns.junit.test.TestHello.class"></property>
<path id="compile-path">
<fileset dir="${lib.dir}" includes="*.jar"></fileset>
</path>
<path id="compile-test-path">
<path refid="compile-path"></path>
<pathelement location="${build.classes}"/>
</path>
<path id="run-test-path">
<path refid="compile-test-path"></path>
<pathelement location="${build.test.classes}"/>
</path>
<target name="clean">
<echo>进行项目清理工作</echo>
<delete dir="${build.dir}"></delete>
</target>
<target name="init" depends="clean">
<echo>进行项目的初始化</echo>
<mkdir dir="${build.dir}"/>
<mkdir dir="${build.classes}"/>
<mkdir dir="${build.test.dir}"/>
<mkdir dir="${build.test.classes}"/>
<mkdir dir="${build.test.report}"/>
</target>
<target name="compile" depends="init">
<echo>编译源文件</echo>
<javac includeantruntime="true" srcdir="${src.dir}" destdir="${build.classes}" classpathref="compile-path"></javac>
</target>
<target name="compile-test" depends="compile">
<echo>编译测试文件</echo>
<javac includeantruntime="true" srcdir="${test.src.dir}" destdir="${build.test.classes}" classpathref="compile-test-path"></javac>
</target>
<target name="run-test" depends="compile-test">
<echo>运行单元测试</echo>
<junit printsummary="true" haltonfailure="true">
<classpath refid="run-test-path"></classpath>
<formatter type="brief" usefile="false"/>
<test name="${run.test.class}"></test>
</junit>
</target>
<target name="end" depends="run-test">
<echo>整个过程结束</echo>
</target>
</project>
运行build.xml出现如下结果:
Buildfile: E:\Workspaces\Eclipse\Junit_ant\build.xml
clean:
[echo] 进行项目清理工作
[delete] Deleting directory E:\Workspaces\Eclipse\Junit_ant\build
init:
[echo] 进行项目的初始化
[mkdir] Created dir: E:\Workspaces\Eclipse\Junit_ant\build
[mkdir] Created dir: E:\Workspaces\Eclipse\Junit_ant\build\classes
[mkdir] Created dir: E:\Workspaces\Eclipse\Junit_ant\build\test
[mkdir] Created dir: E:\Workspaces\Eclipse\Junit_ant\build\test\classes
[mkdir] Created dir: E:\Workspaces\Eclipse\Junit_ant\build\test\report
compile:
[echo] 编译源文件
[javac] Compiling 1 source file to E:\Workspaces\Eclipse\Junit_ant\build\classes
compile-test:
[echo] 编译测试文件
[javac] Compiling 1 source file to E:\Workspaces\Eclipse\Junit_ant\build\test\classes
run-test:
[echo] 运行单元测试
BUILD FAILED
E:\Workspaces\Eclipse\Junit_ant\build.xml:50: java.lang.NoClassDefFoundError: junit/framework/TestResult
Total time: 857 milliseconds
以上测试时单独测试TestHello没有问题,但是使用ant测试时出现了错误
请问是哪里出了问题,调了好久没找到原因!!!! JUnit Ant --------------------编程问答-------------------- 估计是你lib里面没有junit的jar包 --------------------编程问答--------------------
谢谢你的回复,但是我在lib下面引入了junit-4.10.jar和hamcrest-core-1.3.jar两个jar包呢
为什么会找不到呢 --------------------编程问答-------------------- 自己顶一下,希望能有高手帮到 --------------------编程问答-------------------- 我也遇到了同样的问题,你解决了嘛?? --------------------编程问答-------------------- 我用了hamcrest-all包解决了问题 --------------------编程问答-------------------- 没做过ant测试,看错误提示是缺少那个testresult类,,试试楼上说的看看。。 --------------------编程问答-------------------- 明显的缺少包 --------------------编程问答-------------------- 谁制定给个明确的答案啊! --------------------编程问答-------------------- 1、看你截图中存在感叹号,说明需要重新编译。
2、出现这种问题,一般是classpath的设置问题。
你这个任务<target name="run-test" depends="compile-test">中没有包含junit的jar包吧。
3、如果实在不行,直接把junit包放到eclipse的ant设置中。 Window->preferences->Ant->runtime
选择classpath,选择Ant Home Entries,添加junit包 --------------------编程问答-------------------- 另外,再提示一点。有时候设置好了后,需要把工程刷新一下。或者重启一下eclipse,问题就能解决了。 --------------------编程问答--------------------
eclipse里有只能证明在eclipse里可以run junit没有问题,
可build.xml里编译时却没有引用到junit.jar包。。
补充:Java , Java EE