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.
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?