With JUnit I have set up a web-based runner/control unit that allows the end user to access and run the tests.
It is extremely primitive and it would run on the server instead of on their machine but here is the code that I used in order to achieve this:
@Stateless
@Path("test")
class TestsService {
@Context
UriInfo ui;
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("runSpec")
public Result runSpec(@QueryParam("spec") String spec) {
JUnitCore jUnitCore = new JUnitCore();
try {
Class test = Class.forName(spec)
return jUnitCore.run(test);
} catch (Exception ex) {
ex.printStackTrace()
}
}
@GET
@Path("runAll")
public Result runAll() {
JUnitCore jUnitCore = new JUnitCore();
try {
Reflections reflections = new Reflections("Tests.*");
Class[] allClasses =
reflections.getSubTypesOf(Specification.class);
return jUnitCore.run(allClasses);
} catch (Exception ex) {
ex.printStackTrace()
}
}
}
This created a rest layer using jersey, personally using a built-in jetty server. From there I built out an AngularJS page that displayed all of the historically ran Tests from the system.
An example of the jetty server:
public class App {
public static void main(String[] args) throws Exception {
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
FilterHolder holder = new FilterHolder(new CrossOriginFilter());
holder.setInitParameter("allowedMethods", "GET,POST,HEAD,OPTIONS");
context.addFilter(holder, "/*", EnumSet.of(DispatcherType.REQUEST));
Server jettyServer = new Server(9898);
jettyServer.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
// Tells the Jersey Servlet which REST service/class to load.
jerseyServlet.setInitParameter(
"jersey.config.server.provider.classnames",
TestsService.class.getCanonicalName());
try {
jettyServer.start();
jettyServer.join();
} finally {
jettyServer.destroy();
}
}
}