How to make Unit Test servlet?

I have a servlet called Calculator . It reads the left , right and op parameters and returns, setting the result attribute in the response.

What is the easiest way to unit test this: basically I want to create an HttpServletRequest, set the parameters, and then check the answer - but how to do it?

Here's the servlet code (it’s small and silly for its intended purpose):

 public class Calculator extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet { public Calculator() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer left = Integer.valueOf(request.getParameter("left")); Integer right = Integer.valueOf(request.getParameter("right")); Integer result = 0; String op = request.getParameter("operator"); if ("add".equals(op)) result = this.opAdd(left, right); if ("subtract".equals(op)) result = this.opSub(left, right); if ("multiply".equals(op)) result = this.opMul(left, right); if ("power".equals(op)) result = this.opPow(left, right); if ("divide".equals(op)) result = this.opDiv(left, right); if ("modulo".equals(op)) result = this.opMod(left, right); request.setAttribute("result", result); // It'll be available as ${sum}. request.getRequestDispatcher("index.jsp").forward(request, response); } } ... 

}

+4
source share
5 answers

Often, important program logic is taken into account in other classes that can be used in different contexts, rather than being closely related to the Servlet Engine. This leaves the servlet itself as a simple adapter between the website and your application.

This simplifies program testing and simplifies reuse in other contexts, such as desktop or mobile applications.

+12
source

There are several libraries that you can use. Are you using Spring http://www.springsource.org/ in your application? If so, there is one application for Spring (spring -test) that contains the MockHttpServletRequest. For instance:

 @Test public void shouldReturnAValidaRedirectionMessage() { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("op", "addition"); request.addParameter("left", "1"); request.addParameter("right", "5"); CalculatorServlet servlet = new CalculatorServlet(); Operation operation = servlet.getOperation(request); assertNotNull(operation); assertEquals(ADDITION, operation.getOperationType()); ... 
+10
source

Check out ServletUnit. This is part of HttpUnit.

http://httpunit.sourceforge.net/doc/servletunit-intro.html

+1
source

I can’t say that this is the best way to do this: but for a unit test a simple servlet like this (one does not use forwards, context, etc.), you can simply do:

  • Create instances of HttpServletReqeust and HttpServletResponse using any of the mocking libraries. It is even easier to use the RequestWrapper and ResponseWrapper classes (simple custom classes implemented by extending the HttpServletReqeust and HttpServletResponse classes).
  • In these mock (or custom) instances, certain properties are set β€” parameters that you want to test in each test case β€” for example, op=add to add unit test. If you use custom classes, you can simply set them in an internal property object. If you use mocks, then waiting for the settings will do.
  • Create an instance of the servlet - new Calculator() , saving the required libraries in the class path. Now call the service method on this instance.
  • When the call returns, you will get o / p from the response class and approve it. Since the response class is again a regular class or a modified version, this should be easy.

For ridicule, just the starting point would be EasyMock or Mockito (my fav)

Wrapping example: http://tomcat.apache.org/tomcat-5.5-doc/servletapi/javax/servlet/http/HttpServletRequestWrapper.html

NTN

+1
source

Typically, you should abstract your business logic from the details of the servlet container. You can make fun of ServletRequest using the Spring test suite , but it would be nice to simulate a Servlet container. Thus, you must either run system tests on a real container, or transfer your logic from the servlet to a separate bean and test it in isolation.

  public class Calculator { public Integer calculate(Integer left, Integer right, String op) { Integer result = 0; if ("add".equals(op)) result = this.opAdd(left, right); if ("subtract".equals(op)) result = this.opSub(left, right); if ("multiply".equals(op)) result = this.opMul(left, right); if ("power".equals(op)) result = this.opPow(left, right); if ("divide".equals(op)) result = this.opDiv(left, right); if ("modulo".equals(op)) result = this.opMod(left, right); return result; } } public class CalculatorServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Integer left = Integer.valueOf(request.getParameter("left")); Integer right = Integer.valueOf(request.getParameter("right")); String op = request.getParameter("operator"); Integer result = calculator.calculate(left, right, op); request.setAttribute("result", result); // It'll be available as ${sum}. request.getRequestDispatcher("index.jsp").forward(request, response); } } 
0
source

All Articles