How to pass an HttpServletRequest object to a test case?

Now I am writing a test case of my class. I want to pass the parameter of the HttpServletRequest object to my test method to check if this method works or not. Therefore, any of them gives me an offer.

public void testCheckBatchExecutionSchedule() throws Exception { assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request)); } 
+7
source share
5 answers

HttpServletRequest is an interface. I used to just create a class (e.g. TestHttpServletRequest ) that had an empty method body for every method in HttpServletRequest , except for the ones I really need. For most methods, I returned the instance variable and enabled the configuration tool for this instance variable so that the test case could determine what needs to be returned. HttpServletRequest has many methods, but most IDEs (I use Eclipse) can generate method stubs.

The problem with the HttpServletRequestWrapper is that you still need to pass another HttpServletRequest to your constructor to serve as the default for each method. Passing null results in a NullPointerException .

+4
source

Spring provides a class called MockHttpServletRequest that can be used to test code that requires an HttpServletRequest.

 public void testCheckBatchExecutionSchedule() throws Exception { MockHttpServletRequest request = new MockHttpServletRequest(); request.addParameter("parameterName", "someValue"); assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request)); } 
+22
source

you must mock the request object using a mocking library like http://code.google.com/p/mockito/

 public void testCheckBatchExecutionSchedule() throws Exception { HttpServletRequest mockRequest = mock(HttpServletRequest.class); //setup the behaviour here (or do it in setup method or something) when(mockRequest.getParameter("parameterName")).thenReturn("someValue"); assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(mockRequest)); } 
+10
source

Updated February 2018: OpenBrace Limited closed and its ObMimic product is no longer supported.

You can also use the ObMimic library of test duplicates of the Servlet API:

 import com.openbrace.obmimic.mimic.servlet.http.HttpServletRequestMimic; public void testCheckBatchExecutionSchedule() throws Exception { HttpServletRequestMimic request = new HttpServletRequestMimic(); // Configure the request as necessary... // eg request.getMimicState().getRequestParameters().set("name", "value"); assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request)); } 

To configure a request, HttpServletRequestMimic has a getMimicState () method that returns an HttpServletRequestState through which all the necessary details of the request can be configured (and through which you can access any ServletContext, HttpSession, etc., etc., and configure them in a similar way when necessary). The documentation for the HttpServletRequestState includes a summary of its properties and methods, as well as a fully detailed Javadoc .

Note that:

  • ObMimic also provides similar "mock" classes for HttpServletResponse, ServletContext, HttpSession, ServletConfig, etc.

  • A free version of Community Edition by ObMimic is available on the download website.

  • The only library you need to add to your project for this is ObMimic / lib / obmimic.jar (assuming the servlet API itself is already present).

  • The ObMimic website contains complete documentation, including Getting Started , How to leads with sample code, detailed Javadoc , etc.

0
source

Using the API provided by tomcat, you can get an HttpServletRequest object

 HttpServletRequest request = (HttpServletRequest)org.apache.catalina.core.ApplicationFilterChain.getLastServicedRequest(); 

This will get the last request sent to the servlet for servicing from the current thread.

This work is only performed in Tomcats "Strict Servlet Compliance" mode. To enable it, add the following JVM parameter:

 org.apache.catalina.STRICT_SERVLET_COMPLIANCE=true 
-one
source

All Articles