Skip to content

Cory Foy

Organizational agility through intersecting business and technology

Menu
  • FASTER Fridays
  • Mapping Mondays
  • Player Embed
  • Search Videos
  • User Dashboard
  • User Videos
  • Video Category
  • Video Form
  • Video Tag
Menu

Lightweight method for running JUnit servlet tests

Posted on August 26, 2005 by Cory Foy

Brian Slesinsky posted a nice lightweight approach on the XP list about running servlets in the same process as JUnit using Jetty:

One lightweight approach that most people don’t seem to know about is using Jetty to run servlets in the same process as JUnit. Jetty has a much nicer internal API than Tomcat. After writing a wrapper class you can have tests like this:

protected void setUp() {
webserver = new WebServer(); // starts on localhost with an
arbitrary unused port
servlet = webserver.addServlet(MyServlet.class);
}

protected void tearDown() {
webserver.shutdown();
}

You can test using HttpUnit as usual, but it saves Tomcat deploys and you can also access objects in the servlet directly since you’re in the same process.

If your webapp isn’t too complicated, you can also write a main() method to run your webapp without bothering with building a war file.

1 thought on “Lightweight method for running JUnit servlet tests”

  1. Anonymous says:
    June 19, 2006 at 4:02 am

    I am struggling with this just now. What I do not want, is to have Jetty and test code in the same class loader which your solution seems to imply.

    Any thoughts?

Comments are closed.

© 2025 Cory Foy | Powered by Superbs Personal Blog theme