Skip to end of metadata
Go to start of metadata

Jetty is a very lightweight servlet container, it starts up in just a handful of milliseconds, and easily allows you to unit test your HTTP Servlets.

Unfortunately, the Jetty API isn't that easy to navigate. Here is a ServletRunner that will start up a Jetty container, and some useful stuff that is easy to integrate into your unit tests to test the servlets.

This code uses

  • Jetty 7
  • Hamcrest
  • JUnit

First of all, an example:

Notice how the servlet runner is not assigned a port - it will pick a free port, and so allows tests to run concurrently on the same box.

ServletRunnerExample.java

Retrieving a URL

Here is a useful class to allow the retrieval of things at the end of a URL - it probably needs a bit of cleanup... in particular it plays fast and loose with character encodings, and only cares about string content.

UrlGetter.java
HttpResult.java

Asserting about the results

HttpResultMatchers.java

The ServletRunner itself

This has a couple of features:

  • Can run a web application (exploded or war)
  • Can set up global JNDI entries for datasources or whatever
  • Can set servlet / filter init parameters.
ServletRunner.java
Labels: