当前位置:首页 > 开发 > 编程语言 > Spring > 正文

Spring MVC 的单元测试实例

发表于: 2014-08-15   作者:bo_hai   来源:转载   浏览次数:
摘要: 一、先定义实例: public class BaseTest { private static HandlerMapping handlerMapping; private static HandlerAdapter handlerAdapter; @BeforeClass public static void setUp() { String[] config

一、先定义实例:

public class BaseTest {

	private static HandlerMapping handlerMapping;  
	private static HandlerAdapter handlerAdapter;

	@BeforeClass
	public static void setUp() {
		String[] configs = {
				"classpath:spring/spring-mvc-servlet.xml"
		};
		
		XmlWebApplicationContext context = new XmlWebApplicationContext();
		context.setConfigLocations(configs);
		MockServletContext msc = new MockServletContext();
		context.setServletContext(msc);
		context.refresh();
		msc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE,context);
		handlerMapping = (HandlerMapping) context.getBean(RequestMappingHandlerMapping.class);
		handlerAdapter = (HandlerAdapter) context.getBean(context.getBeanNamesForType(RequestMappingHandlerAdapter.class)[0]);     
	}
	
	public ModelAndView excuteAction(HttpServletRequest request,HttpServletResponse response) throws Exception {
		HandlerExecutionChain chain = handlerMapping.getHandler(request);
		final ModelAndView model = handlerAdapter.handle(request, response,chain.getHandler());
		return model;
	}
}

 二、单元测试方法:

	@Test
	public void test() throws Exception{
	    MockHttpServletRequest request = new MockHttpServletRequest();  
	    MockHttpServletResponse response = new MockHttpServletResponse();  
	    request.setServletPath("/inputUserInfo");  
	    //request.addParameter("id", "1002");  
	    //request.addParameter("date", "2010-12-30");  
	    request.setMethod("GET"); // "POST" 
	    // 执行URI对应的action  
	    final ModelAndView mav = this.excuteAction(request, response);  
	    // Assert logic  
	    Assert.assertEquals("view/inputUserInfo", mav.getViewName());  
	    String msg=(String)request.getAttribute("msg");  
	    //System.out.println(msg); 
	}

  三、个人认为实用性不强。

Spring MVC 的单元测试实例

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
Spring MVC作为SpringFrameWork的产品,自诞生之日,就受到广泛开发者的关注,如今Spring MVC在Java
Spring MVC是spring的一个子模块,用于方便web应用的开发。以下是如何配置和使用spring mvc的一个简
Spring MVC是spring的一个子模块,用于方便web应用的开发。以下是如何配置和使用spring mvc的一个简
转至:http://blog.csdn.net/dingx/article/details/2242105 Spring Framework可以被使用在很多场合
Spring MVC作为SpringFrameWork的产品,自诞生之日,就受到广泛开发者的关注,如今Spring MVC在Java
概述: springmvc 框架围绕DispatcherServlet这个核心展开,DispatcherServlet是Spring MVC的总控制
示例下载地址:http://download.csdn.net/detail/geloin/4506640 本文基于Spring 注解,让Spring跑
示例下载地址:http://download.csdn.net/detail/geloin/4506640 本文基于Spring 注解,让Spring跑
示例下载地址:http://download.csdn.net/detail/geloin/4506640 本文基于Spring 注解,让Spring跑
玩转单元测试之 Testing Spring MVC Controllers 转载注明出处:http://www.cnblogs.com/wade-xu/p/
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号