Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.

Testing Web Applications with Jetty

31 Jan 2010

An embedded Jetty server provides a quick and easy means of testing web applications. In this example I expand on A Secure RESTful Web Service, which requires manual steps of building and deploying a web application to an existing and configured application server. I introduce an embedded Jetty server which is started as part of testing, and enables tests to run without the manual steps of building and deploying the web application. It also eliminates the need to have a discrete application server available for testing.

The first step is to add an embedded Jetty server to a test case, configured to start prior to the tests and stop once they are complete.

private static Server server;

public static void startWebapp() throws Exception {
  server = new Server();

  Connector connector = new SelectChannelConnector();


  WebAppContext webAppContext = new WebAppContext();




public static void stopWebapp() throws Exception {

The only changes to EmployeeControllerTest are to add two methods which run before and after the test case execution, setting up and tearing down the Jetty server, as well as a reference to the Server object itself.



The Maven configuration is changed to add Jetty and a couple of dependencies.

That's all there is to it. Now when EmployeeControllerTest is run, it will first set up the Jetty server and deploy the web application from its location within the source tree, run the tests, and take down the Jetty server. This comes at the cost of only a few seconds, and makes testing much easier to automate.

Something went wrong with that request. Please try again.