tags:

views:

91

answers:

3

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

+2  A: 

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/"/>
matt b
Shouldn't Java descend automatically in that folder? And if I have 20 packages for tests, I need to add them all in the classpath?
Geo
@Geo For folders, it needs start at the package root. Currently junit thinks the class should be unit_tests.testpackage.TestMyClass instead of testpackage.TestMyClass
Kevin
@Kevin, I tried to run a class by it's own, using the `test` task, and it works. It's using the same classpath as the one posted by me. Any ideas why that could be?
Geo
Might be helpful to post the configuration that works to figure out why the original does not
matt b
It's exactly the same.
Geo
Kevin, based on your comment with the `unit_tests.testpackage.TestMyClass` package suggestion, I made it work. When I put only `COMPILED_CLASSES` on the fileset, it worked. If you care to write an answer, I'll mark it.
Geo
So you changed your package name to match your Ant configuration?
matt b
No, I only removed the `unit_tests` from the string passed to the `dir` property of fileset. Kevin's comment ( and running a single test with the `test` task ) made me realize the bug.
Geo
A: 

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

Rob Spieldenner
+1  A: 

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

Rob Spieldenner