Source:How do servlets work? Instantiation, sessions, shared variables and multithreading
public class MyTestClass {
public static void main(final String[] args) throws Exception {
final JUnitCore junit = new JUnitCore();
final String singleTest = // Get the name of test from somewhere (environment, system property, whatever you want).
final Request req;
if (singleTest != null) {
req = Request.method(MyTestClass.class, singleTest);
} else {
req = Request.aClass(MyTestClass.class);
}
final Result result = junit.run(req);
// Check result.getFailures etc.
if (!result.wasSuccessful()) {
System.exit(1);
}
}
// Your @Test methods here.
}