This question already has an answer here:
-
Creating a mock HttpServletRequest out of a url string?
4 answers
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 ?
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");
}
}
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
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
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.
Have a look at Mockrunner: http://mockrunner.sourceforge.net/
It has a lot of easy to use Java EE mocks, including HttpServletRequest and HttpServletResponse.