[英]Junit executing via ANT keep getting Class not found Exception
我已经为此进行了搜索和Google搜索,但无法正常工作! 该脚本编译并创建了我的jar文件,没问题,我可以从Eclipse运行单元测试,但是在使用ANT构建器运行时会遇到Class Not Found异常。 这是Junit报告的完整错误:
java.lang.ClassNotFoundException: CustomerTest
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:340)
at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:36)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:452)
at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:139)
这是build.xml段
<property name="src_dir" value="src/com/biscom/fadb8"/>
<property name="build" value="bin"/>
<property name="classes_dir" value="${build}/classes"/>
<property name="dist" value="dist"/>
<property name="lib_dir" value="lib"/>
<property name="reports" value="${build}/junitreports"/>
<property name="junit_dir" value="c:\junit"/>
<path id="classpath">
<fileset dir="${lib_dir}" includes="**/*.jar"/>
<pathelement location="${junit_dir}/junit-4.12.jar"/>
<pathelement location="${junit_dir}/hamcrest-core-1.3.jar"/>
</path>
<path id="classpath_junit">
<fileset dir="${lib_dir}" includes="**/*.jar"/>
<pathelement location="${junit_dir}/junit-4.12.jar"/>
<pathelement location="${junit_dir}/hamcrest-core-1.3.jar"/>
<pathelement location="${build}/classes"/>
</path>
<target name="junit" depends="createdist">
<junit printsummary="yes">
<classpath>
<path refid="classpath_junit"/>
<path refid="application"/>
</classpath>
<formatter type="xml"/>
<batchtest fork="no" todir="${reports}">
<fileset dir="${src_dir}" includes="CustomerTest.java"/>
</batchtest>
</junit>
<junitreport todir="${reports}">
<fileset dir="${reports}" includes="TEST-*.xml"/>
<report todir="${reports}"/>
</junitreport>
</target>
这是CustomerTest类:
public class CustomerTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public final void testGetAccounts()
{
ArrayList<Customer> list = Customer.getAccounts(Customer.OFFICE, Customer.STATUS_NONE);
org.junit.Assert.assertNotNull("should not be null", list);
int counter = list.size();
org.junit.Assert.assertTrue(counter > 0 );
}
@Test
public final void testGetAccountsInvArgs()
{
ArrayList<Customer> list = Customer.getAccounts(5, Customer.STATUS_NONE);
org.junit.Assert.assertNull("should be null", list);
}
}
这是Customer.java的相关部分:
public class Customer
{
public static ArrayList<Customer> getAccounts(int accountType, int status)
{
logger.info("getAccounts. start");
if (accountType < Customer.OFFICE || accountType > Customer.ENTERPRISE)
{
logger.error("getAccounts. invalid accountType: " + accountType);
return null;
}
ArrayList<Customer> list = null;
PreparedStatement statement = null;
StringBuffer sqlStmt = new StringBuffer(500);
....
....
return list;
}
您的CustomerTest类显然不在类路径中。 您是否检查过$ {build} / classes目录? 如果您使用的是Maven项目设置之类的东西,那么您的测试实际上可能会编译到$ {build} / test-classes之类的目录中。
更新请参阅: https : //ant.apache.org/manual/Tasks/junit.html#nested
该链接建议您使用:
<batchtest fork="no" todir="${reports}">
<fileset dir="${src_dir}" includes="**/CustomerTest.java"/>
</batchtest>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.