您的位置:首页 > 编程语言 > Java开发

利用Spring的mock进行单元测试

2009-05-09 14:41 330 查看

与测试独立的Java对象相比,测试J2EE Web组件是一项更困难的任务,因为Web组件必须运行在Web容器里,并且必须与HTTP请求交互才有意义。mock对象是一个术语,主要流行于eXtreme程序员和JUnit小组中。在单元测试中,mock对象以简单方式模仿某个组件的行为和结果,从而保证单元测试专注于组件本身,而不用担心其他依赖性问题。

1. Spring的mock类
Spring框架提供大量测试用的mock类,包括JNDI相关的mock类,Spring portlet相关的mock类,以及Web应用相关的mock类。尤其是Web应用相关的mock类,可以大大提高Web组件测试的便捷性。以下是Spring所提供的Web应用相关的mock类。
² org.springframework.mock.web.MockHttpServletRequest :HttpServletRequest接口的mock实现,用于模拟客户端的HTTP请求。这是最重要、最常用的mock类。
² org.springframework.mock.web.MockHttpServletResponse:HttpServletResponse接口的mock实现,用于模拟服务器端对客户端的响应。
² org.springframework.mock.web.MockHttpSession:HttpSession接口的mock实现,这是一个经常使用的mock类。
² org.springframework.mock.web.DelegatingServletInputStream:ServletInputStream接口的mock实现。
² org.springframework.mock.web.DelegatingServletOutputStream:ServletOutputStream接口的mock实现,如果需要拦截和分析服务器的输出流内容,可以使用该类。
² org.springframework.mock.web.MockFilterConfig:FilterConfig接口的mock实现。
² org.springframework.mock.web.MockPageContext:JSP PageContext接口的mock实现,通过该类可以测试预编译的JSP。
² org.springframework.mock.web.MockRequestDispatcher:RequestDispatcher接口的一个mock实现。
² org.springframework.mock.web.MockServletConfig:ServletConfig接口的一个mock实现。
测试用例中,可以简单的创建这些mock对象来模拟目的对象。例如HttpServletRequest是一个保存HTTP参数的组件,而这些参数可用于驱动其他Web组件。MockHttpServletRequest可以直接创建HttpServletRequest接口的实例,并设置其中的参数。在典型的Web组件测试下,可以采用如下方式设置其中的任何参数:
//指定表单方法和请求的URL
MockHttpServletRequest request = new MockHttpServletRequest("POST","/login.do");
//向请求中增加参数
request.addParameter("user","username");
request.addParameter("pass","password");
类似的地,也可以在测试用例中创建HttpServletResponse和HttpSession对象,并设置它们的相关属性。
2. 利用mock类测试控制器
下面是利用mock对象完成控制器测试的源代码:详见mockTest
// 该控制器是MultiActionController控制器的delegate类
public class SampleDelegate
{
// welcome方法
public ModelAndView welcome( HttpServletRequest req, HttpServletResponse resp)
{
System.out.println("==========" + req.getParameter("method"));
return new ModelAndView("/WEB-INF/jsp/welcome.jsp", "model", new Long(System.currentTimeMillis()));
}
// hello方法
public ModelAndView hello( HttpServletRequest req, HttpServletResponse resp)
{
return new ModelAndView("/WEB-INF/jsp/hello.jsp", "model", "欢迎学习Spring");
}
}
下面是关于此控制器的配置文件。配置文件里配置了MultiActionController,同时配置了该控制器的delegate。配置文件如下:

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">

<beans>
<!--
ParameterMethodNameResolver - 解析请求参数,并将它作为方法名(http://www.sf.net/index.view?testParam=testIt的请求就会调用 testIt(HttpServletRequest,HttpServletResponse))。使用paramName配置参数可以调整所检查的参数

InternalPathMethodNameResolver - 从路径中获取文件名作为方法名(http://www.sf.net/testing.view的请求会调用testing(HttpServletRequest, HttpServletResponse)方法)

PropertiesMethodNameResolver - 使用用户定义的属性对象将请求的URL映射到方法名。当属性定义/index/welcome.html=doIt,

并且收到/index/welcome.html的请求,就调用doIt
-->

<!-- 配置根据请求名决定控制器-->
<bean id="handlerMapping"
class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>

<!-- 配置MultiActionController 控制器所需要的参数解析器 -->
<bean id="paramResolver" class="org.springframework.web.servlet.mvc.multiaction.ParameterMethodNameResolver">
<!-- 确定根据method来决定处理器定向 -->
<property name="paramName">
<value>method</value>
</property>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: