KOPIS.DE

Auf diesen Seiten findest Du die Projekte und Blogposts von Carsten Ringe.

Using Mortbays Jetty as embedded servlet container in unit tests

26 October 2009

Ich habe diesen Blogeintrag in einem Firmenblog veröffentlicht, daher ist er in Englisch. Und bitte:

Today I created a simple unit test that runs Mortbays Jetty as an embedded servlet container for a unit test with JUnit 4. It's quite simple to run the ServletTester and add Servlets to it, so you can create HTTP requests and assert against the responses.

To run Jetty you need the following JAR files in your classpath:

  • jetty-6.X.Y.jar
  • jetty-servlet-tester-6.X.Y.jar
  • jetty-util-6.X.Y.jar
  • servlet-api-2.5-*.jar

The HelloServletTest simply sets up Jetty to initialize a servlet, runs one test and stops Jetty after it. The HelloServlet I used for the first test. It's the obvious Hello World example.

Now you can remove HelloServlet</code>, add your own servlet classes and go unit test them. ;-)</p>