Run JUnit Test from a java web application

2019-07-14 15:44发布

问题:

Is it possible to run JUnit Tests from a java web application (on a Tomcat server) ?

How can I with a click on a link, launch the instantiation of an object ? or the call to a method ?

Thank you very much.

Best Regards.

回答1:

Sure, although I'm not sure why you'd want to.

See the cookbook example, nutshell:

org.junit.runner.JUnitCore.runClasses(TestClass1.class, ...);

Obviously you'd need to deploy your test classes so they're available on the classpath, and convert from a string class name to an actual Class.

You'd likely also need to scan for annotations or convention-based class names; there are a variety of ways to do that including things like the reflections library.

IMO it'd be easier to set up a continuous integration (CI) server like Jenkins or CruiseControl etc. and get a complete package, but it depends on what your needs actually are.



回答2:

There a number of JUnit-based frameworks that can assist with web testing.

Jwebunit

Canoo Webtest

HttpUnit

HtmlUnit