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 ?
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