Ant, ClassNotFoundException with JUnit



我得到ClassNotFoundException与JUnit使用Ant,

我的Ant代码是:

<path id ="classpath">
    <fileset dir="code/lib" includes="**/*.jar" />
</path>
<target name="clean">
    <delete dir="code/build"/>
    <delete dir="code/Sauvegardes"/>
</target>
<target name="build" >
    <mkdir dir="code/build"/>
    <mkdir dir="code/Sauvegardes"/>
    <javac includeantruntime="false" srcdir="code/main" destdir="code/build" classpathref="classpath" />
</target>

<target name="run" depends="build" >
    <java classname="GameMain" 
    classpath="code/build" fork="true"/>
</target>
<target name="test" depends="build">
    <junit printsummary="yes" showoutput="yes">
        <classpath>
            <pathelement path="code/lib/junit-4.11.jar"/>
            <pathelement path="code/lib/hamcrest-core-1.3.jar"/>
            <pathelement path="build"/>
        </classpath>
        <batchtest >
            <fileset dir="code" includes="test.java"/>
        </batchtest>
        <formatter type="plain" usefile="false" />
    </junit>
</target>

得到如下输出:

test:
    [junit] Running test
    [junit] Testsuite: test
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] Tests run: 1, Failures: 0, Errors: 1, Time elapsed: 0 sec
    [junit] 
    [junit]     Caused an ERROR
    [junit] test
    [junit] java.lang.ClassNotFoundException: test
    [junit]     at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    [junit]     at java.security.AccessController.doPrivileged(Native Method)
    [junit]     at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    [junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
    [junit]     at java.lang.Class.forName0(Native Method)
    [junit]     at java.lang.Class.forName(Class.java:270)
    [junit] 
    [junit] Test test FAILED

如何解决这个问题?

您将不得不编译您的测试,并将测试类文件夹添加到junit类路径中。我建议您在code下创建一个test文件夹。

<target name="build-test" >
    <mkdir dir="code/test-classes"/>
    <javac includeantruntime="false" srcdir="code/test" destdir="code/test-classes" classpathref="classpath" />
</target>
<target name="test" depends="build, build-test">
    <junit printsummary="yes" showoutput="yes">
        <classpath>
            <pathelement path="code/lib/junit-4.11.jar"/>
            <pathelement path="code/lib/hamcrest-core-1.3.jar"/>
            <pathelement path="code/build"/>
            <pathelement path="code/test-classes"/>
        </classpath>
        <batchtest >
            <fileset dir="code/test" includes="test.java"/>
        </batchtest>
        <formatter type="plain" usefile="false" />
    </junit>
</target>

相关内容

  • 没有找到相关文章