user391549
user391549

Reputation: 33

Ant JUnit Task couldn't find junit/framework/TestCase.class

I'm trying to get a JUnit 4.8.1 task to run in Ant 1.7.1. My IDE is Eclipse Helios. I've been banging my head against a brick wall for 2 days now and cannot figure this out. I'm sure from reading other posts its a classpath problem, but I can't see where I'm going wrong.

My JUnit4 test suite is defined as follows:

package mypackage.tests;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

/**
 * JUnit 4 Test Suite for the entire <code>mypackage</code>
 * package
 */
@RunWith(Suite.class)
@Suite.SuiteClasses({ mypackage.tests.controller.AllTests.class })
public class AllTests {
}

...simple enough, but the test fails with a ClassNotFoundException

java.lang.ClassNotFoundException: mypackage.tests.AllTests
 at java.net.URLClassLoader$1.run(Unknown Source)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at java.lang.Class.forName0(Native Method)
 at java.lang.Class.forName(Unknown Source)

When I switch on the debug flag, I get the following trace from Ant:

(...)
[junit] Couldn't find junit/framework/TestCase.class
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
fileset: Setup scanner in dir C:\eclipse\plugins with patternSet{ includes: [org.junit_4.8.1.v4_8_1_v20100427-1100/junit.jar] excludes: [] }
Finding class junit.framework.Test
Loaded from C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar junit/framework/Test.class
(...)

Clearly the JUnit jar is on the classpath, and other classes such as Test.class are being picked up, so why am I getting the 'couldn't find TestClass.class' message?

I tried adding the JUnit.jar to the ant classpath in the Eclipse GUI, and this has the following effect:

(...)
[junit] Found C:\eclipse\plugins\org.junit_4.8.1.v4_8_1_v20100427-1100\junit.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-launcher.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant.jar
[junit] Found C:\eclipse\plugins\org.apache.ant_1.7.1.v20100518-1145\lib\ant-junit.jar
(...)
[junit] WARNING: multiple versions of ant detected in path for junit 
[junit]          jar:file:C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
[junit]      and jar:file:/C:/eclipse/plugins/org.apache.ant_1.7.1.v20100518-1145/lib/ant.jar!/org/apache/tools/ant/Project.class
(...)

Can someone please please please help me to get this working!

Oh, I almost forgot... the test suite runs fine from within Eclipse.

Thanks in advance!

Upvotes: 3

Views: 9527

Answers (1)

jens
jens

Reputation: 1862

The error is caused by the fact that your test classes cannot be loaded.

Did you put the package mypackage.tests.controller (i.e. the folder(s) containing the class files or the jar archive) on the classpath of your junit call? This can be done using a nested classpath structure. (http://ant.apache.org/manual/Tasks/junit.html).

Upvotes: 2

Related Questions