In-process Web Integration Tests with Jetty and JWebUnit
Do you speak test? In that case: Hello web application:
public class WebIntegrationTest extends net.sourceforge.jwebunit.WebTestCase {
public void testIndex() {
beginAt("/index.html");
assertTextPresent("Hello world");
}
private org.mortbay.jetty.Server server;
protected void setUp() throws Exception {
server = new org.mortbay.jetty.Server(0);
server.addHandler(
new org.mortbay.jetty.webapp.WebAppContext("src/main/webapp", "/my-context"));
server.start();
int actualPort = server.getConnectors()[0].getLocalPort();
getTestContext().setBaseUrl("http://localhost:" + actualPort + "/my-context");
}
}
This code runs with no application server, no separate deployment step, just like that.
If this looks interesting, see my full-sized article on java.net
This work is licensed under a
Creative Commons Attribution 3.0 License.
Print This Post
Kristoffer Said,
January 2, 2007 @ 8:09 am
I can’t get the example up and running from within my favourite IDE(A) after generating the project files with mvn idea:idea because web.xml is not conformant to the Servlet 2.4 specification.
Ok, so I know this is a bit childish :) but I guess you’ve been using some other IDE of which doesn’t provide proper XSD validation(?).
Anyhow, the following fragment (slightly revised) is valid:
<servlet-mapping>
<servlet-name>my</servlet-name>
<url-pattern>/my/*</url-pattern>
</servlet-mapping>
Kristoffer Said,
January 3, 2007 @ 8:04 am
One thing worth mentioning (at least for IntelliJ users) is that if you implement tests like Johannes describes in the blog and put them into a sub-module in a multi-module Maven2 project, you will have to explicitly specify the working directory for that module when running the integration test(s). If not you will get FileNotFoundExceptions followed by HTTP 503s for whatever resource you try to hit afterwards.
Arnljot.com » Bark and CubicTest Said,
January 10, 2007 @ 1:10 am
[…] Christian and I spoke of a few things, mostly how it would be interesting to run the Watir ruby scripts in JRuby inside CubicTest to get a tighter integration allowing the ruby scripts to directly report results to CubicTest. And we also discussed how exporters are made since I might be interested in writing a JWebUnit exporter considering this article about integration of Jetty and JWebUnit. […]