通过ANT执行的Junit保持未找到类异常



我已经搜索并在谷歌上搜索过了,但无法让它工作!脚本编译并创建了我的jar文件,没有问题我可以从Eclipse运行我的单元测试,但在使用ANT构建器运行时会出现ClassNOT 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类的目录中。

更新请参阅:https://ant.apache.org/manual/Tasks/junit.html#nested

此链接建议您使用:

     <batchtest fork="no" todir="${reports}">
        <fileset dir="${src_dir}" includes="**/CustomerTest.java"/>
     </batchtest>

相关内容

  • 没有找到相关文章

最新更新