jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How can I use JUnit to test a method that takes a complex parameter - like an HttpServletRequest?
Topics Tools, Tools:Debugging, Process:Patterns, Tools:Testing:JUnit
Author Tom Copeland
Created Jul 25, 2000 Modified Jul 26, 2000


Answer
Say you want to test this method:
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...
	}


Is this item helpful?  yes  no     Previous votes   Yes: 5  No: 0



Comments and alternative answers

Comment on this FAQ entry

Mock Object framework works for this
Todd Folsom, May 3, 2002
The mock object framework, from www.mockobjects.com, provides this null-object functionality without you having to code it yourself. The framework provides the methods for setting up the mocks and verifying their state after your test runs. You can use the mockmaker tool, from www.mockmaker.org, to create new mocks, such as MockHttpServletRequest.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Jakarta Cactus, HttpUnit
Sean Sullivan, Jun 30, 2002
Try:

http://jakarta.apache.org/cactus/

http://www.httpunit.org/



Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
Excellent answer
Hiren Pathak, May 18, 2005
Thanks for the answer. It gave nice reference for writing test cases.

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  
getParameterNames()
David Sperling, Jul 15, 2006
Shouldn't the answer above read like this:
    public Enumeration getParameterNames(){
        return this.parameters.keys();
    }

not like this:
    public Enumeration getParameterNames(){
        return this.parameters.elements();
    }

Cheers,

Is this item helpful?  yes  no     Previous votes   Yes: 0  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

Tools FAQ

Tools Forum

Java 2 SDK Tools and Utilities

Patterns FAQ

Patterns Forum

Design Patterns Home Page

Huston Design Patterns

Brad Appleton's Software Patterns Links

Object-Oriented Bibliography

JUnit FAQ

JUnit Forum

JUnit Homepage

Wish List
Features
About jGuru
Contact Us

 


Internet.com
The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers