Anthony Anthony -3 years ago 135
Java Question

Creating a mock HttpServletRequest out of a url string?

I have a service that does some work on an HttpServletRequest object, specifically using the request.getParameterMap and request.getParameter to construct an object.

I was wondering if there is a straightforward way to take a provided url, in the form of a string, say

String url = "";

and easily convert it to a HttpServletRequest object so that I can test it with my unit tests? Or at least just so that request.getParameterMap and request.getParameter work correctly?

Answer Source

Spring has MockHttpServletRequest in its spring-test module.

If you are using maven you may need to add the appropriate dependency to your pom.xml. You can find spring-test at

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download