Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass the HttpServletRequest object to the test case? [duplicate]

Now I am writing the test case of my class. I want to pass the HttpServletRequest object parameter to my test case method to check whether the method is working or not. So any one give me the suggestion to that.

public void testCheckBatchExecutionSchedule() throws Exception     {         assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request));     } 
like image 680
Kallar_Mannargudi Avatar asked Jul 30 '12 12:07

Kallar_Mannargudi


People also ask

Can we clone the HttpServletRequest?

The request and response classes aren't designed to be cloned or accessed from multiple threads. If you try to do so, you're bound to run into problems.

Which method of the HttpServletRequest object is used to get the value?

getParameter. Returns the value of a request parameter as a String , or null if the parameter does not exist. Request parameters are extra information sent with the request. For HTTP servlets, parameters are contained in the query string or posted form data.

How HttpServletRequest object is created?

In a typical Servlet container implementation if an HTTP request comes in, an HttpServletRequest is created right when the HTTP input data of the request is parsed by the Servlet container.

What is the difference between ServletRequest and HttpServletRequest?

The ServletRequest and HttpServletRequest classes hold all of the accessible information about the client and the server. HttpServletRequest is a subclass of ServletRequest, and is passed to each servlet handler method (such as doGet(..), doPut(..), etc.)


1 Answers

Spring provides a class called MockHttpServletRequest, which can be used to test code that needs a HttpServletRequest.

public void testCheckBatchExecutionSchedule() throws Exception {    MockHttpServletRequest request = new MockHttpServletRequest();    request.addParameter("parameterName", "someValue");    assertTrue("Batch is Completed :", returnPointsRatingDisputeFrom.checkBatchExecutionSchedule(request)); } 
like image 190
Stefan Birkner Avatar answered Sep 16 '22 16:09

Stefan Birkner