使用Ant和JUnit的ClassNotFoundException

时间:2015-03-20 12:23:37

标签: ant

这是我的代码:

    <project name="SampleProject" default="test-html" basedir=".">

    <property name="ws.home" value="${basedir}" />
    <property name="ws.jars" value="E:/jars" />
    <property name="classes" value="${ws.home}/build" />
    <property name="test.src" value="${ws.home}/src" />
    <property name="test.reportsDir" value="c:/report" />

    <!-- classpath -->
    <path id="test.classpath">
        <fileset dir="${ws.jars}" includes="*.jar" />
    </path>

    <!-- clean -->
    <target name="clean" description="clean up">
        <delete dir="${classes}" />
        <echo message="Directory deleted..." />
    </target>

    <!-- create -->
    <target name="create" description="create">
        <mkdir dir="${classes}" />
        <echo message="Directory created..." />
    </target>

    <!-- compile -->
    <target name="compile" depends="clean,create">
        <echo message="compiling..." />
        <javac debug="true" srcdir="${test.src}" destdir="${classes}"
            includeantruntime="false" classpathref="test.classpath" />
        <echo message="compilation completed..." />
    </target>

    <!-- generating HTML-Report -->
    <target name="test-html" depends="compile">

        <junit fork="yes" printsummary="yes" haltonfailure="no">
            <classpath refid="test.classpath" />
            <batchtest fork="yes" todir="${test.reportsDir}">
                <fileset dir="${classes}">
                    <include name="testcases/FirstTestCase.class" />
                </fileset>
            </batchtest>
            <formatter type="xml" />
            <classpath refid="test.classpath" />
        </junit>

        <junitreport todir="${test.reportsDir}">
            <fileset dir="${test.reportsDir}">
                <include name="TEST-*.xml" />
            </fileset>
            <report todir="${test.reportsDir}" />
        </junitreport>
    </target>
</project>

我在使用命令提示符生成的报告中遇到以下问题:

testcases.FirstTestCase

java.lang.ClassNotFoundException: testcases.FirstTestCase
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)

我在Eclipse中运行build.xml时遇到以下错误:

Buildfile: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build.xml
clean:
   [delete] Deleting directory E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
     [echo] Directory deleted...
create:
    [mkdir] Created dir: E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
     [echo] Directory created...
compile:
     [echo] compiling...
    [javac] Compiling 1 source file to E:\JLC REVISION\ANT WITH JUNIT\SampleProject\build
     [echo] compilation completed...
test-html:
    [junit] Running testcases.FirstTestCase
    [junit] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0 sec
    [junit] Test testcases.FirstTestCase FAILED
[junitreport] Processing c:\report\TESTS-TestSuites.xml to C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
[junitreport] Loading stylesheet jar:file:/C:/Student%20Dvd/Student%20DVD/ANT/apache-ant-1.9.4-bin/apache-ant-1.9.4/lib/ant-junit.jar!/org/apache/tools/ant/taskdefs/optional/junit/xsl/junit-frames.xsl
[junitreport] Transform time: 2172ms
[junitreport] Deleting: C:\DOCUME~1\MUKESH~1\LOCALS~1\Temp\null1997459403
BUILD SUCCESSFUL
Total time: 5 seconds

我从这里尝试了很多,但我没有得到解决方案。帮助我。

1 个答案:

答案 0 :(得分:0)

看起来你编译的类不在你的测试类路径上。

javac编译为${classes},但该目录未包含在test.classpath的定义中。

尝试添加dir作为元素:

<path id="test.classpath">
    <fileset dir="${ws.jars}" includes="*.jar" />
    <pathelement path="${classes}" />
</path>