Spring MVC测试框架详解――服务端测试(一)

2014-11-24 03:11:22 · 作者: · 浏览: 9

随着RESTful Web Service的流行,测试对外的Service是否满足期望也变的必要的。从Spring 3.2开始Spring了Spring Web测试框架,如果版本低于3.2,请使用spring-test-mvc项目(合并到spring3.2中了)。

Spring MVC测试框架提供了对服务器端和客户端(基于RestTemplate的客户端)提供了支持。

对于服务器端:在Spring 3.2之前,我们测试时一般都是直接new控制器,注入依赖,然后判断返回值。但是我们无法连同Spring MVC的基础设施(如DispatcherServlet调度、类型转换、数据绑定、拦截器等)一起测试,另外也没有现成的方法测试如最终渲染的视图(@ResponseBody生成的JSON/XML、JSP、Velocity等)内容是否正确。从Spring 3.2开始这些事情都可以完成了。而且可以测试完整的Spring MVC流程,即从URL请求到控制器处理,再到视图渲染都可以测试。

对于客户端:不需要启动服务器即可测试我们的RESTful 服务。

1 服务器端测试

我的环境:JDK7、Maven3、spring4、Servlet3

首先添加依赖

如下是spring-context和spring-webmvc依赖:

        
  
            
   
    org.springframework
   
            
   
    spring-context
   
            
   
    ${spring.version}
   
        
  

        
  
            
   
    org.springframework
   
            
   
    spring-webmvc
   
            
   
    ${spring.version}
   
        
  

版本信息: 4.0.0.RELEASE

如下是测试相关的依赖(junit、hamcrest、mockito、spring-test):

        
  
            
   
    junit
   
            
   
    junit
   
            
   
    ${junit.version}
   
            
   
    test
   
        
  

        
  
            
   
    org.hamcrest
   
            
   
    hamcrest-core
   
            
   
    ${hamcrest.core.version}/version> 
    
     test
     
   
  
        
  
            
   
    org.mockito
   
            
   
    mockito-core
   
            
   
    ${mockito.core.version}
   
            
   
    test
   
        
  

        
  
            
   
    org.springframework
   
            
   
    spring-test
   
            
   
    ${spring.version}
   
            
   
    test
   
        
  
版本信息: 4.11 1.3 1.9.5

然后准备测试相关配置

实体:

package com.sishuok.mvc.entity;
import java.io.Serializable;
public class User implements Serializable {
    private Long id;
    private String name;
    //省略getter/setter等
}

控制器:

package com.sishuok.mvc.controller;
//省略import
@Controller
@RequestMapping(/user)
public class UserController {

    @RequestMapping(/{id})
    public ModelAndView view(@PathVariable(id) Long id, HttpServletRequest req) {
        User user = new User();
        user.setId(id);
        user.setName(zhang);

        ModelAndView mv = new ModelAndView();
        mv.addObject(user, user);
        mv.setViewName(user/view);
        return mv;
    }
}

XML风格配置:

spring-config.xml:加载非web层组件


  

  
    
   
    
   
    
    
     
    
   

  

spring-mvc.xml:加载和配置web层组件


  

  
    
   
    
   
    
    
     
    
   
    
    
     
     
      jsp//"> 
       
      
     
     vc:annotation-driven>
   
  

web.xml配置:此处就不贴了,请前往github查看。

对于context:component-scan注意事项请参考《context:component-scan扫描使用上的容易忽略的use-default-filters》和《第三章 DispatcherServlet详解 ——跟开涛学SpringMVC》。

等价的注解风格配置:

AppConfig.java:等价于spring-config.xml

package com.sishuok.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;

@Configuration
@ComponentScan(basePackages = com.sishuok.mvc, excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ANNOTATION, value = {Controller.class})
})
public class AppConfig {
}

MvcConfig.java:等价于spring-mvc.xml

package com.sishuok.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.view.InternalResourceViewRes