views:

508

answers:

3

I saw Jetty has got and ServletTest class which you can use to test Servlets.

tester = new ServletTester();
tester.setContextPath("/");
tester.addServlet(TestServlet.class, "/servlet/*");
... 
tester.start();

Do you know if there is something similar in Tomcat? How could I test a servlet?

+4  A: 

Take a look at Jakarta Cactus

Cactus is a simple test framework for unit testing server-side java code (Servlets, EJBs, Tag Libs, Filters, ...).

Here's a servlet test how-to

Bozho
+4  A: 

HttpUnit has a has a "simulated container" called ServletUnit.

dbrown0708
A: 

I've never found a benefit to testing servlets directly (nor Struts actions, say), especially given the work needed to do it.

Most of my servlets/actions/whatever use POJOs for the bulk of their work, and the POJOs are heavily tested. The webapps themselves have suites of HtmlUnit tests. Everything in between I assume to be just plumbing.

I don't believe that I've even once encountered any sort of bug that would have ONLY been caught by testing the servlet classes directly, and which would not be caught by the POJO or webapp tests.

Rodney Gitzel