开发者

Trouble running JUnit from Ant

Here's the target I'm using to run my tests:

<target name="run-tests" description="run the tests" depends="compilation">
        <junit>
            <sysproperty key="tests.basedir" value="${SPECIAL_PATH}/unit_tests"/>
            <classpath>
                <pathelement location="${COMPILED_CLASSES}"/>
                <pathelement location="${basedir}/junit-4.8.1.jar"/>
            </classpath>
            <batchtest>
                <fileset dir="${COMPILED_CLASSES}/unit_tests/">
                    <include name="**/Test*.class"/>
                    <exclude name="**/*$*"/>
                </fileset>
            </batchtest>
        </junit>
    </target>

However, every time I try to run this target, all my tests fail with something like:

[junit] java.lang.ClassNotFoundException: testpackage.TestMyClass
[junit]     at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
[junit]     at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
[junit]     at java.lang.Class.forName0(Native Method)
[junit]     开发者_Python百科at java.lang.Class.forName(Class.java:247)

The SPECIAL_PATH property points to the source code of the classes. The COMPILED_CLASSES property points to the place the .class files have been put. And I need the tests.basedir property because I use it from my unit tests.

What am I doing wrong?

EDIT:I also thought I should explain the exclude of the $. I'm excluding anonymous classes, because they don't represent TestCases, they're only used from them.


You are telling Junit to execute each test class in ${COMPILED_CLASSES}/unit_tests/ but you are putting just ${COMPILED_CLASSES} on the classpath. You probably need to change your classpath entry to

<pathelement location="${COMPILED_CLASSES}/unit_tests/"/>


Since your claspath has

${COMPILED_CLASSES}

and your test classes are in

${COMPILED_CLASSES}/unit_tests

they would need to be in package

unit_tests.<whatever the classpath is>

traditionally this is why people compile normal sources to target/classes and test sources to target/test-classes


You might need to compile the UnitTests. Could you post where you use the javac task?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜