The following code allows you to run real web applications using Jetty inside unit tests. It does so by loading your web.xml (the code belows assumes it is a Maven based web project - if not then the src/main/webapp location properly needs to be changed). This is quite nice since it relieves you of having to hand wire servlets and parameters in your unit tests or even worse, resolving to those pesky Servlet Mock tests that was needed in the past.

// start server
@BeforeClass
public void setUp() throws Exception {
server = new Server(port);
WebAppContext webAppContext =
new WebAppContext("src/main/webapp", "/");
webAppContext.setConfigurationClasses(new String[] {
"org.mortbay.jetty.webapp.WebInfConfiguration",
"org.mortbay.jetty.webapp.WebXmlConfiguration" });
server.addHandler(webAppContext);
server.start();
}

// shutdown server
@AfterClass
public void tearDown() throws Exception {
if (server != null)
server.stop();
}
This allows for real service unit tests, by using such tools as HtmlUnit and Selenium to test HTML/Ajax interfaces and Commons HttpClient (with XPATH) to test XML services (REST/SOAP). Since your entire application is instrumented, the unit test can create full code coverage reports by using tools like:

* Clover
* Emma
* Cobertura

Nifty stuff.

0 comments



Recommended Money Makers