문제

I'm getting ClassNotFoundException with JUnit using Ant,

My Ant code is :

<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>

And I get the following output:

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

How do I solve this ?

도움이 되었습니까?

해결책

You will have to compile your test(s) and add the test classes folder to the junit classpath. I suggest you create a test folder under code.

<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>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top