39

I have a function that looks for a query parameter and returns a boolean:

  public static Boolean getBooleanFromRequest(HttpServletRequest request, String key) {
        Boolean keyValue = false;
        if(request.getParameter(key) != null) {
            String value = request.getParameter(key);
            if(keyValue == null) {
                keyValue = false;
            }
            else {
                if(value.equalsIgnoreCase("true") || value.equalsIgnoreCase("1")) {
                    keyValue = true;
                }
            }
        }
        return keyValue;
    }

I have both junit and easymock in my pom.xml, how do I go about mocking the HttpServletRequest ?

Blankman
  • 259,732
  • 324
  • 769
  • 1,199
  • 3
    The below answer this question as written well, but I would normally advocate for refactoring the code so that most of the non-trivial logic is in at a more appropriate level of abstraction. If that's possible, then mocking HttpServletRequest becomes unnecessary. – James Kingsbery Jul 31 '14 at 21:15

5 Answers5

28

Use some mocking framework e.g. Mockito or JMock which comes with mocking capacity of such objects.

In Mockito, you can do mocking as:

 HttpServletRequest  mockedRequest = Mockito.mock(HttpServletRequest.class);

For details on Mockito, see: How do I drink it? on the Mockito site.

In JMock, you can do mocking as :

 Mockery context = new Mockery();
 HttpServletRequest  mockedRequest = context.mock(HttpServletRequest.class);

For details on jMock, please refer: jMock - Getting Started

Mr. Lance E Sloan
  • 3,297
  • 5
  • 35
  • 50
Yogendra Singh
  • 33,927
  • 6
  • 63
  • 73
15

HttpServletRequest is much like any other interface, so you can mock it by following the EasyMock Readme

Here is an example of how to unit test your getBooleanFromRequest method

// static import allows for more concise code (createMock etc.)
import static org.easymock.EasyMock.*;

// other imports omitted

public class MyServletMock
{
   @Test
   public void test1()
   {
      // Step 1 - create the mock object
      HttpServletRequest req = createMock(HttpServletRequest.class);

      // Step 2 - record the expected behavior

      // to test true, expect to be called with "param1" and if so return true
      // Note that the method under test calls getParameter twice (really
      // necessary?) so we must relax the restriction and program the mock
      // to allow this call either once or twice
      expect(req.getParameter("param1")).andReturn("true").times(1, 2);

      // program the mock to return false for param2
      expect(req.getParameter("param2")).andReturn("false").times(1, 2);

      // switch the mock to replay state
      replay(req);

      // now run the test.  The method will call getParameter twice
      Boolean bool1 = getBooleanFromRequest(req, "param1");
      assertTrue(bool1);
      Boolean bool2 = getBooleanFromRequest(req, "param2");
      assertFalse(bool2);

      // call one more time to watch test fail, just to liven things up
      // call was not programmed in the record phase so test blows up
      getBooleanFromRequest(req, "bogus");

   }
}
karth
  • 635
  • 3
  • 13
  • 25
Guido Simone
  • 7,912
  • 2
  • 19
  • 21
11

This is an old thread ... but the question is still relevant.

Another good choice is MockServiceRequest and MockServiceResponse in the Spring framework:

http://docs.spring.io/spring/docs/2.0.x/api/org/springframework/mock/web/package-summary.html

FoggyDay
  • 11,962
  • 4
  • 34
  • 48
2

I don't know about easymock, but the book 'Unit Testing in Java: How Tests Drive the Code' by Johannes Link contained explanations of how to test Servlets using a library he'd build of dummy objects.

The companion site for the book is now gone (change in publishing company of something...) but the companion site from the original german publication is still up. From it, you can download the definitions of all the dummy objects.

Richard JP Le Guen
  • 28,364
  • 7
  • 89
  • 119
0

Have a look at Mockrunner: http://mockrunner.sourceforge.net/

It has a lot of easy to use Java EE mocks, including HttpServletRequest and HttpServletResponse.

Arjan Tijms
  • 37,782
  • 12
  • 108
  • 140
mbelow
  • 1,093
  • 6
  • 11