Run JUnit Test from a java web application

2019-07-14 15:27发布

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.

2条回答
淡お忘
2楼-- · 2019-07-14 16:20

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.

查看更多
啃猪蹄的小仙女
3楼-- · 2019-07-14 16:21

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

Jwebunit

Canoo Webtest

HttpUnit

HtmlUnit

查看更多
登录 后发表回答