diff --git a/build.xml b/build.xml
index 7ca30bd49..d31fafc6a 100644
--- a/build.xml
+++ b/build.xml
@@ -298,7 +298,7 @@
junit
supports a nested <classpath>
element, that represents a PATH like
-structure. The value is ignored if fork
is
-disabled.
JUnit is a framework to create unit test. It has been initially * created by Erich Gamma and Kent Beck. JUnit can be found at http://www.xprogramming.com/software.htm. + * href="http://www.junit.org">http://www.junit.org. * - *
This ant task runs a single TestCase. By default it spans a new
- * Java VM to prevent interferences between different testcases,
- * unless fork
has been disabled.
+ *
To spawn a new Java VM to prevent interferences between
+ * different testcases, you need to enable fork
.
*
* @author Thomas Haas
- * @author Stefan Bodewig
+ * @author Stefan Bodewig
*/
public class JUnitTask extends Task {
@@ -217,9 +217,24 @@ public class JUnitTask extends Task {
Project.MSG_WARN);
}
- JUnitTestRunner runner =
- new JUnitTestRunner(test, test.getHaltonerror(),
- test.getHaltonfailure());
+ JUnitTestRunner runner = null;
+
+ Path classpath = commandline.getClasspath();
+ if (classpath != null) {
+ log("Using CLASSPATH " + classpath, Project.MSG_VERBOSE);
+ AntClassLoader l = new AntClassLoader(project, classpath,
+ false);
+ // make sure the test will be accepted as a TestCase
+ l.addSystemPackageRoot("junit");
+ // will cause trouble in JDK 1.1 if omitted
+ l.addSystemPackageRoot("org.apache.tools.ant");
+ runner = new JUnitTestRunner(test, test.getHaltonerror(),
+ test.getHaltonfailure(), l);
+ } else {
+ runner = new JUnitTestRunner(test, test.getHaltonerror(),
+ test.getHaltonfailure());
+ }
+
if (summary) {
log("Running " + test.getName(), Project.MSG_INFO);
diff --git a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
index 9d9d3e7ce..1f3806299 100644
--- a/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
+++ b/src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java
@@ -75,7 +75,7 @@ import java.util.Vector;
*
*
Summary output is generated at the end. * - * @author Stefan Bodewig + * @author Stefan Bodewig */ public class JUnitTestRunner implements TestListener { @@ -135,14 +135,31 @@ public class JUnitTestRunner implements TestListener { */ private JUnitTest junitTest; + /** + * Constructor for fork=true or when the user hasn't specified a + * classpath. + */ public JUnitTestRunner(JUnitTest test, boolean haltOnError, boolean haltOnFailure) { + this(test, haltOnError, haltOnFailure, null); + } + + /** + * Constructor to use when the user has specified a classpath. + */ + public JUnitTestRunner(JUnitTest test, boolean haltOnError, + boolean haltOnFailure, ClassLoader loader) { this.junitTest = test; this.haltOnError = haltOnError; this.haltOnFailure = haltOnFailure; try { - Class testClass = Class.forName(test.getName()); + Class testClass = null; + if (loader == null) { + testClass = Class.forName(test.getName()); + } else { + testClass = loader.loadClass(test.getName()); + } try { Method suiteMethod= testClass.getMethod("suite", new Class[0]);