It seems that runClasses() doesn’t terminate the code being tested even after the test times out and fails.
An SSCCE:
import org.junit.Test;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
class Main {
public static void main(String[] args) {
Class<?>[] classes = { PublicTest.class };
Result result = JUnitCore.runClasses(classes);
System.out.println("Num tests: " + result.getRunCount());
System.out.println("Time: " + result.getRunTime());
System.out.println("Failure: " + result.getFailures().get(0));
}
}
Test class:
import org.junit.Test;
public class PublicTest {
@Test(timeout=10)
public void loop() {
while(true) {}
}
}
How do I get the code to terminate?
JUnitCore at some point indeed does create a new thread that is never finishing. The main() implementation of JUnit avoids getting stuck by calling System.exit() when it has completed its work. That seems a little untidy to me, but might be the best solution for you.