Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to mock the HttpServletRequest? [duplicate]

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 ?

like image 497
Blankman Avatar asked Oct 18 '12 01:10

Blankman


People also ask

How do I get HttpServletRequest in Junit?

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.

What is the use of HttpServletRequestWrapper?

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.

What is MockHttpServletRequest?

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.

What is HttpServletRequest spring boot?

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.


2 Answers

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

like image 121
Yogendra Singh Avatar answered Oct 24 '22 14:10

Yogendra Singh


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");     } } 
like image 34
Guido Simone Avatar answered Oct 24 '22 15:10

Guido Simone