Simplest ways to mock an HttpServletRequest
:
Create an anonymous subclass:
HttpServletRequest mock = new HttpServletRequest ()
{
private final Map<String, String[]> params = /* whatever */
public Map<String, String[]> getParameterMap()
{
return params;
}
public String getParameter(String name)
{
String[] matches = params.get(name);
if (matches == null || matches.length == 0) return null;
return matches[0];
}
// TODO *many* methods to implement here
};
Use jMock, Mockito, or some other general-purpose mocking framework:
HttpServletRequest mock = context.mock(HttpServletRequest.class); // jMock
HttpServletRequest mock2 = Mockito.mock(HttpServletRequest.class); // Mockito
Use HttpUnit's ServletUnit and don't mock the request at all.