InsertionTest.java
package efms.db.dao.impl;
import java.util.Arrays;
import java.util.Collection;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
@RunWith(value = Parameterized.class)
public class InsertionTest {
int num1;
int num2;
int num3;
public InsertionTest(int num1, int num2, int num3) {
super();
this.num1 = num1;
this.num2 = num2;
this.num3 = num3;
}
@Parameters
public static Collection<Object[]> data() {
Object[][] data = new Object[][] { { 1, 1, 2 }, { 2, 2, 4 } };
return Arrays.asList(data);
}
@Test
public void testSummationPositive() {
Assert.assertEquals("Sum should be 4", num1 + num2, num3);
}
}
的build.xml
<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." default="all" name="Junit TestCase Execution">
<property name="p" location="" />
<property name="prj" value="" />
<path id="tests.classpath">
<pathelement location="${p}/${prj}/build/classes" />
<pathelement location="${p}/lib/junit.jar" />
<pathelement location="${p}/lib/hamcrest-core-1.3.jar" />
<pathelement location="${p}/lib/../*.jar" />
</path>
<target name="compile">
<echo message="${p}/${prj}/build/classes" />
<javac srcdir="${p}" destdir="${p}/${prj}/build/classes" classpathref="tests.classpath">
<classpath>
<pathelement location="${p}/lib/**/*.jar" />
</classpath>
<include name="**/InsertionTest.java" />
</javac>
</target>
<target name="junit" depends="compile">
<echo message="${p}/${prj}/build/classes/" />
<junit fork="yes" printsummary="yes">
<formatter type="xml" />
<!-- Add all your test class names here. -->
<formatter type="plain" />
<batchtest todir="${p}/${prj}/build/classes">
<fileset dir="${p}/${prj}/build/classes/">
<include name="**/InsertionTest.*" />
</fileset>
</batchtest>
<classpath>
<pathelement location="${p}/lib/junit.jar" />
<pathelement location="${p}/lib/hamcrest-core-1.3.jar" />
</classpath>
</junit>
</target>
<target name="report" depends="junit">
<junitreport todir="${p}/${prj}/build/classes">
<fileset dir="${p}/${prj}/build/classes">
<include name="TEST-*.xml" />
</fileset>
<report format="frames" todir="${p}/${prj}/build/classes" />
</junitreport>
<record name="${p}/${prj}/build/classes/test-output.txt" action="start" />
</target>
<target name="all" depends="compile,junit,report">
</target>
</project>
错误:
efms.db.dao.impl.InsertionTest
java.lang.ClassNotFoundException: efms.db.dao.impl.InsertionTest
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
我编写了一个简单的junit测试用例,当我运行build.xml时,文件正在编译但是在运行junit测试用例时它显示错误。我猜build.xml存在一些问题。我能帮忙解决这个问题吗?
编译时:
[javac] Compiling 1 source file to C:\Users\IBM_ADMIN\workspace1\Efms3.3\build\classes
junit:
[echo] C:\Users\IBM_ADMIN\workspace1\Efms3.3//build/classes/
[junit] Running efms.db.dao.impl.InsertionTest
[junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
[junit] Test efms.db.dao.impl.InsertionTest FAILED
report:
[junitreport] Processing C:\Users\IBM_ADMIN\workspace1\Efms3.3\build\classes\TESTS-TestSuites.xml to C:\Users\IBM_AD~1\AppData\Local\Temp\null2013296782
[junitreport] Loading stylesheet jar:file:/C:/Rahul/RahulDrive/Software/Development%20Tools/Apache/apache-ant-1.9.2-bin/apache-ant-1.9.2/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 491ms
[junitreport] Deleting: C:\Users\IBM_AD~1\AppData\Local\Temp\null2013296782
all:
BUILD SUCCESSFUL
Total time: 3 seconds
答案 0 :(得分:1)
您的Ant代码表示用于运行测试的类路径是:
<classpath>
<pathelement location="${p}/lib/junit.jar" />
<pathelement location="${p}/lib/hamcrest-core-1.3.jar" />
</classpath>
您需要在类路径中添加更多内容:
请注意,您可以引用其他地方定义的路径,而不是复制所有内容:
<path refid="tests.classpath"/>
有关详细信息,请参阅path documentation