ServletTestHelper for the Time Poor

  1. How do I test my servlet is producing the correct output?
  2. How do I test my servlet is forwarding to the correct page?
  3. How do I test my servlet is redirecting to the correct page?
  4. How do I test my servlet is returning the correct HTTP status code/message?
  5. How do I inject parameter values into my servlet?
  6. How do I test my servlet has set the correct attribute?
  7. How do I test my servlet is writing the correct HTTP headers?

How do I test my servlet is producing the correct output?

Servlet

1 public class HtmlOutputServlet extends HttpServlet {
2     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
3         response.setContentType("text/html");
4         final PrintWriter writer = response.getWriter();
5         writer.print("<html><body><h1>Hello World!</body></html>");
6         writer.close();
7     }
8 }

Test Case

01 public class HtmlOutputServletTest extends TestCase {
02 
03     public void testServletOutput() throws IOException, ServletException {
04         HtmlOutputServlet servlet = new HtmlOutputServlet();
05         ServletTestHelper helper = new ServletTestHelper(servlet);
06 
07         helper.getResponse().setExpectedContentType("text/html");
08 
09         helper.testDoGet();
10 
11         assertEquals("<html><body><h1>Hello World!</body></html>", helper.getResponse().getOutputStreamContents());
12     }
13 }

How do I test my servlet is forwarding to the correct page?

Servlet

1 public class ForwardServlet extends HttpServlet {
2     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
3         request.getRequestDispatcher("index.jsp").forward(request, response);
4     }
5 }

Test Case

01 public class ForwardServletTest extends TestCase {
02     public void testServletOutput() throws IOException, ServletException {
03         ForwardServlet servlet = new ForwardServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         helper.getRequest().setExpectedRequestDispatcherURI("index.jsp");
07         
08         helper.testDoGet();
09 
10     }
11 }

How do I test my servlet is redirecting to the correct page?

Servlet

1 public class RedirectServlet extends HttpServlet {
2     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
3         response.sendRedirect("http://www.corvine.org");
4     }
5 }

Test Case

01 public class RedirectServletTest extends TestCase {
02     public void testServletOutput() throws IOException, ServletException {
03         RedirectServlet servlet = new RedirectServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         helper.getResponse().setExpectedRedirect("http://www.corvine.org");
07         
08         helper.testDoGet();
09     }
10 }

How do I test my servlet is returning the correct HTTP status code/message?

Servlet

1 public class ErrorServlet extends HttpServlet {
2     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
3         response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "I'm sorry Dave - I cant do that.");
4     }
5 }

Test Case

01 public class ErrorServletTest extends TestCase {
02     public void testCorrectErrorCodeReturned() throws IOException, ServletException {
03         ErrorServlet servlet = new ErrorServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         helper.getResponse().setExpectedError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "I'm sorry Dave - I cant do that.");
07         
08         helper.testDoGet();
09 
10     }
11 }

How do I inject parameter values into my servlet?

Servlet

01 public class ParameterQueryServlet extends HttpServlet {
02     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
03         String name = request.getParameter("name");
04 
05         PrintWriter writer = response.getWriter();
06         writer.println(name);
07         writer.close();
08     }
09 }

Test Case

01 public class ParameterQueryServletTest extends TestCase {
02     public void testServletOutput() throws IOException, ServletException {
03         ParameterQueryServlet servlet = new ParameterQueryServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         helper.getRequest().setupAddParameter("name""Andy Marks");
07         
08         helper.testDoGet();
09 
10         assertEquals("Andy Marks\n", helper.getResponse().getOutputStreamContents());
11     }
12 }

How do I test my servlet has set the correct attribute?

Servlet

01 public class ModifyAttributeServlet extends HttpServlet {
02     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
03 
04         String bar = "bar";
05         request.setAttribute("foo", bar);
06         request.setAttribute("foo2"new AttributeValue("bar"));
07     }
08 
09 }
01 public class AttributeValue {
02     private String name;
03 
04     public AttributeValue(String name) {
05         this.name = name;
06     }
07 
08     public int hashCode() {
09         return name.hashCode();
10     }
11 
12     public boolean equals(Object o) {
13         AttributeValue value = (AttributeValueo;
14 
15         if (value == null) {
16             return false;
17         }
18 
19         String otherName = value.getName();
20         if (this.name == null) {
21             return (otherName == null);
22         }
23 
24         return this.name.equals(otherName);
25     }
26 
27     private String getName() {
28         return name;
29     }
30 }

Test Case

01 public class ModifyAttributeServletTest extends TestCase {
02     public void testServletSetAttribute() throws IOException, ServletException {
03         ModifyAttributeServlet servlet = new ModifyAttributeServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         AttributeValue value = new AttributeValue("bar");
07 
08         helper.getRequest().addExpectedSetAttribute("foo2", value);
09         helper.getRequest().addExpectedSetAttribute("foo""bar");
10 
11         helper.testDoGet();
12 
13     }
14 }

Notes

How do I test my servlet is writing the correct HTTP headers?

Servlet

1 public class ModifyHeaderServlet extends HttpServlet {
2     protected void doGet(HttpServletRequest request, HttpServletResponse responsethrows ServletException, IOException {
3         response.setHeader("Cache-Control""no-cache");
4     }
5 }

Test Case

01 public class ModifyHeaderServletTest extends TestCase {
02     public void testServletSetAttribute() throws IOException, ServletException {
03         ModifyHeaderServlet servlet = new ModifyHeaderServlet();
04         ServletTestHelper helper = new ServletTestHelper(servlet);
05 
06         helper.getResponse().setExpectedHeader("Cache-Control""no-cache");
07 
08         helper.testDoGet();
09 
10     }
11 }

Introduction

It is not my intention to explain the reasons for using Mock Object testing techniques. Nor do I plan explaining the intricacies of the framework itself - mostly because you don't have time and partially because I don't profess to completely understand them all myself. This guide is purely for people who want to quickly start writing tests using this framework.

I'll add extra examples as time passes and I dig further into the innards of the ServletTestHelper and it's associated classes.

Andy Marks - October 16th, 2004.

Development Environment

Links