mardi 6 septembre 2016

How to create an instance of HttpServletRequest for unit testing?

While doing some searches on SO I came across this piece of code to extract the "appUrl" from a URL:

public static String getAppUrl(HttpServletRequest request)
{
     String requestURL = request.getRequestURL().toString();
      String servletPath = request.getServletPath();
      return requestURL.substring(0, requestURL.indexOf(servletPath));
}

My question is how does one unit test something like this? The key problem is how to create an instance of HttpServletRequest for unit testing?

Fwiw I tried some googling and most of the responses center around mocking the class. But if I mock the class so that getRequestURL 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.

Aucun commentaire:

Enregistrer un commentaire