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.
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.
Asserting about the results
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.