Anthony Anthony - 6 months ago 20
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 = "http://www.example.com/?param1=value1&param";


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?

pap pap
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 mvnrepository.com.

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