I am currently developing a program that can execute JUnit test cases on external classes. These external classes are sent in by students and we would like to evaluate them.
I have the following test case
import static org.junit.Assert.*;
import org.junit.Test;
public class Task1Test {
@Test
public void testAdd() {
Task1 t = new Task1();
int a = 5;
int b = 11;
assertEquals("Wrong add result", a+b, t.add(a,b));
}
}
and I compiled it with:
$ javac -cp .:../lib/junit/junit-4.11.jar Task1Test.java
The Task1
will be a student's class, but for now it is just a sample class with an add
method that will return a wrong result. The file Task1.java
is located in the same folder as Task1Test.java
.
In my program I load the test case class and try to run it with JUnitCore
:
String testsPath = "/path/to/classes";
String junitJar = "/path/to/junit-4.11.jar"
URL taskUrl = new File(testsPath).toURI().toURL();
URL[] urls = new URL[] {taskUrl,junitJar};
@SuppressWarnings("resource")
ClassLoader loader = new URLClassLoader(urls);
Class<?> clazz = loader.loadClass(task);
Result res = JUnitCore.runClasses(clazz);
if(!res.wasSuccessful()) {
for(Failure f : res.getFailures()) {
System.out.println(f.toString());
}
}
However, it does not work as expected. When I run this code, I get this message:
initializationError(Task1Test): No runnable methods
When I look into the loaded class using reflections, I can see that the method testAdd
has no annotation (i.e. method.getAnnotation(org.junit.Test.class)
returns null
).
Does anyone have an idea? Did I forget a compiler switch or anything?
I am using Java 1.7.0_11
and the code is run in an web application on Glassfish 3.1.2.2
EDIT:
I can run the test case from command line with:
$ java -cp .:../../code/lib/junit/junit-4.11.jar:../../code/lib/junit/hamcrest-core-1.3.jar org.junit.runner.JUnitCore Task1Test
I found a solution from this answer I did not set a parent class loader, which seems to have caused the trouble. Setting it as it was said in the other answer it now executes the test.