title: How to create an instance of HttpServletRequest for unit testing?
body: <p>While doing some searches on SO I came across <a href="https://stackoverflow.com/a/10407294/147530">this</a> piece of code to extract the "appUrl" from a URL:</p>
  
  <pre><code>public static String getAppUrl(HttpServletRequest request)
  {
       String requestURL = request.getRequestURL().toString();
        String servletPath = request.getServletPath();
        return requestURL.substring(0, requestURL.indexOf(servletPath));
  }
  </code></pre>
  
  <p>My question is how does one unit test something like this? The key problem is how to create an instance of <code>HttpServletRequest</code> for unit testing?</p>
  
  <p>Fwiw I tried some googling and most of the responses center around mocking the class. But if I mock the class so that <code>getRequestURL</code> returns what I want it to return (taking an example since mocking essentially overrides some methods to return canned values), then I am not really testing the code at that point. I also tried the httpunit library but that also does not help.</p>