How can I use JUnit to test a method that takes a complex parameter - like an HttpServletRequest?
Created May 4, 2012
public Vector getEmployeeList(HttpServletRequest req) { Vector list = new Vector(); String department = req.getParameter("departmentID"); // get the employees in the specified department... return list; }
We need to pass in something that implements the HttpServletRequest interface. We don't want to go digging into the Tomcat source code or somewhere else to try to find a class that already implements this interface.
One answer is to use a variation on the NullObject pattern. This allows you to implement an interface to do only what you need to do - and everything else on the interface simply returns null, false, or 0. For example, here's a NullObject implementation of HttpServletRequest:
public class NullHttpServletRequest implements HttpServletRequest { public Hashtable parameters = new Hashtable(); public void setParameter(String key, String value) { parameters.put(key, value); } public String getParameter(String key) { return (String)this.parameters.get(key); } public Enumeration getParameterNames(){ return this.parameters.elements(); } public Cookie[] getCookies() {return null;} public String getMethod(){return null;} public String getRequestURI(){return null;} public String getServletPath(){return null;} public String getPathInfo(){return null;} public String getPathTranslated(){return null;} public String getQueryString(){return null;} public String getRemoteUser(){return null;} public String getAuthType(){return null;} public String getHeader(String name){return null;} public int getIntHeader(String name){return 0;} public long getDateHeader(String name){return 0;} public Enumeration getHeaderNames(){return null;} public HttpSession getSession(boolean create){return null;} public String getRequestedSessionId(){return null;} public boolean isRequestedSessionIdValid(){return false;} public boolean isRequestedSessionIdFromCookie(){return false;} public boolean isRequestedSessionIdFromUrl(){return false;} public int getContentLength(){return 0;} public String getContentType(){return null;} public String getProtocol(){return null;} public String getScheme(){return null;} public String getServerName(){return null;} public int getServerPort(){return 0;} public String getRemoteAddr(){return null;} public String getRemoteHost(){return null;} public String getRealPath(String path){return null;} public ServletInputStream getInputStream() throws IOException{return null;} public String[] getParameterValues(String name){return null;} public Enumeration getAttributeNames(){return null;} public Object getAttribute(String name){return null;} public HttpSession getSession(){return null;} public BufferedReader getReader() throws IOException{return null;} public String getCharacterEncoding(){return null;} public void setAttribute(String name, Object o) {} public boolean isRequestedSessionIdFromURL() {return false;} }
Note that some methods have been added to allow you to set some initial parameters in the request.
So you can use this class in a JUnit test case like this:
public void testGetEmployeeList() { AnObject o = new AnObject(); NullHttpServletRequest req = new NullHttpServletRequest(); req.setParameter("department", "2"); Vector list = o.getEmployeeList(req); // code to ensure employees are in department 2... }