簡介
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中的.xml spring-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
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/root-context.xml</param-value> </context-param> <!-- Creates the Spring Container shared by all Servlets and Filters --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <filter> <filter-name>csrfFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>csrfFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- Processes application requests --> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> <async-supported>true</async-supported> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Disables Servlet Container welcome file handling. Needed for compatibility with Servlet 3.0 and Tomcat 7.0 --> <welcome-file-list> <welcome-file></welcome-file> </welcome-file-list> </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()); } }