Spring MVC Controller 单元测试

简介

Controller层的单元测试可使得应用的可靠性获得提高,虽然这使得开发的时间有所增长,有得必失,这里我认为获得的比失去的多不少。java

Sping MVC3.2版本以后的单元测试方法有所变化,随着功能的提高,单元测试更加的简单高效。git

这里以4.1版本为例,记录Controller的单元测试流程。很是值得参考的是Spring MVC Showcase(https://github.com/spring-projects/spring-mvc-showcase),它当前的版本使用的是4.1.0,之后会有所变更,为了使项目可以运行,请以它更新的配置为参考。github

我用的IDE是IntelliJ IDEA13,我我的认为比Eclipse好用不少,是付费的,很贵!web

 

项目结构

使用maven构建项目,项目结构以下:spring

在Controller层的测试不须要写单独的Spring config,能够直接使用src/main/java中的.xmlspring-mvc

这里记录一下,Spring Mvc context的配置策略:mvc

好多的小伙伴都会在一个文件(e.g spring-mvc.xml)中配置不少的东西,来看看showcase中web的配置结构app

  • WEB-INF
    • web.xml (文件)
    • spring (文件夹)webapp

      • root-context.xml (文件,放置可以被servlet和filter共享使用的资源)
      • appServlet (目录)
        • controllers.xml(文件,与Controller相关的配置)
        • servlet-context.xml (文件,放置有servlet使用的资源)

Spring mvc是对Servlet的包装,使其可以结构化,流程化。async

 1 <web-app xmlns="http://java.sun.com/xml/ns/javaee"
 2 
 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4 
 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
 6 
 7 version="3.0">
 8 
 9 <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
10 
11 <context-param>
12 
13 <param-name>contextConfigLocation</param-name>
14 
15 <param-value>/WEB-INF/spring/root-context.xml</param-value>
16 
17 </context-param>
18 
19 <!-- Creates the Spring Container shared by all Servlets and Filters -->
20 
21 <listener>
22 
23 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
24 
25 </listener>
26 
27 <filter>
28 
29 <filter-name>csrfFilter</filter-name>
30 
31 <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
32 
33 <async-supported>true</async-supported>
34 
35 </filter>
36 
37 <filter-mapping>
38 
39 <filter-name>csrfFilter</filter-name>
40 
41 <url-pattern>/*</url-pattern>
42 
43 </filter-mapping>
44 
45 <!-- Processes application requests -->
46 
47 <servlet>
48 
49 <servlet-name>appServlet</servlet-name>
50 
51 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
52 
53 <init-param>
54 
55 <param-name>contextConfigLocation</param-name>
56 
57 <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
58 
59 </init-param>
60 
61 <load-on-startup>1</load-on-startup>
62 
63 <async-supported>true</async-supported>
64 
65 </servlet>
66 
67 <servlet-mapping>
68 
69 <servlet-name>appServlet</servlet-name>
70 
71 <url-pattern>/</url-pattern>
72 
73 </servlet-mapping>
74 
75 <!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 -->
76 
77 <welcome-file-list>
78 
79 <welcome-file></welcome-file>
80 
81 </welcome-file-list>
82 
83 </web-app>

 

能够看到分开配置,使得文件的做用更加的明了。

这部分的配置文件是来配置web context 的,项目中还有其余的module ,如DAO,Service,他们对应的applicationContext文件会被放在src/main/resource目录下。

完善的单元测试固然还有service的单元测试,这里就不说了,可是Controller的单元测试还须要调用service和DAO,要注意Service和DAO的applicationContext的引入。

Controller 单元测试

在测试类中包含这三个注释,看起表面意思不难理解他们的做用,主要理解ContextConfiguration的使用。

@RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration //默认是src/main/webapp

@ContextConfiguration("file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml")

注意:这里的@ContextConfiguration只解析了servlet-context.xml,若是项目中还存在其余模块的applicationContext,也须要把他们引进来不然获得的Service就是null的。

例如

@ContextConfiguration({

"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",

“classpath*: springxml/**.xml”

})

在加上其余的一点代码就能够完成一个Controller的单元测试,下面是一个例子,更多例子请参考showcase中的内容。

package pairwinter.spring.mvc.controller.test;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import org.junit.Before;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.samples.mvc.AbstractContextControllerTests;

import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringJUnit4ClassRunner.class)

@WebAppConfiguration

@ContextConfiguration({

"file:src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml",

“classpath*: springxml/**.xml”

})

public class ControllerTests{

@Autowired

private WebApplicationContext wac;

private MockMvc mockMvc;

@Before

public void setup() throws Exception {

this.mockMvc = webAppContextSetup(this.wac).build();

}

@Test

public void controllerExceptionHandler() throws Exception {

this.mockMvc.perform(get("/test"))

.andExpect(status().isOk());

}

}
相关文章
相关标签/搜索