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 ?
First of all, we will create a new servlet. To create that, we simply right click on project name -> New -> Other -> Servlet under Web. Click on the Next button to proceed.
Class HttpServletRequestWrapper. Provides a convenient implementation of the HttpServletRequest interface that can be subclassed by developers wishing to adapt the request to a Servlet. This class implements the Wrapper or Decorator pattern. Methods default to calling through to the wrapped request object.
public class MockHttpServletRequest extends Object implements HttpServletRequest. Mock implementation of the HttpServletRequest interface. The default, preferred Locale for the server mocked by this request is Locale. ENGLISH . This value can be changed via addPreferredLocale(java.
Spring Boot HttpServletRequest Logging Filter. A Request Logging Filter for Spring Boot applications, that allows you to read the request body multiple times. The request body is cached using an overridden HttpServletRequestWrapper. Then a reader that allows the client to read the body multiple times is returned.
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
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"); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With