javascript
Spring MVC:测试简介
測試是軟件開發中最重要的部分之一。 井井有條的測試有助于使應用程序代碼保持良好狀態,并且處于工作狀態。 有很多不同類型的測試和方法。 在本文中,我想對基于Spring MVC的應用程序進行單元測試進行介紹。 不要希望在這里閱讀有關Spring MVC測試的全部內容,因為這只是有關單元測試的第一篇文章。
談到沒有某些應用程序的單元測試,我要測試的是欺騙。 我將使用上一篇文章中的應用程序之一 ,以避免產生閑聊。 一世
建議您在繼續閱讀當前文章之前對應用程序進行簡短概述。 本教程的主要目的是演示如何在注釋管理器中為Spring MVC應用程序配置單元測試。
準備工作
開始任何開發之前,我們始終要做的第一件事–在Maven的pom.xml文件中添加新的依賴項。 這種情況也不例外。
...<dependency><groupid>org.springframework</groupid><artifactid>spring-test</artifactid><version>${spring.version}</version><scope>test</scope></dependency><dependency><groupid>org.springframework</groupid><artifactid>spring-test-mvc</artifactid><version>1.0.0.M1</version><scope>test</scope></dependency> ...<repositories><repository><id>spring-maven-milestone</id><name>Spring Maven Milestone Repository</name><url>http://maven.springframework.org/milestone</url></repository></repositories> ...我添加了兩個新的依賴項:
第一個是支持使用JUnit和TestNG等工具測試Spring應用程序。 通常為集成測試框架和單元測試存根定義一個“測試”范圍的工件。 第二個用于測試基于Spring MVC服務器端和客戶端的基于RestTemplate的代碼。 請注意,我添加了新的存儲庫。 我這樣做是因為spring-test-mvc仍然不在官方Maven存儲庫中。
單元測試的控制器
在這篇文章中,我將為最簡單的控制器編寫兩個單元測試。 這是控制器的代碼:
@Controller public class LinkController { @RequestMapping(value="/") public ModelAndView mainPage() { return new ModelAndView("home"); } @RequestMapping(value="/index") public ModelAndView indexPage() { return new ModelAndView("home"); } }因此,您可以看到控制器中的方法很簡單,它們只是返回一些JSP。 控制器的測試意味著檢查請求狀態(成功的情況下,代碼應為200)并驗證視圖名稱。
編寫Spring MVC的單元測試
這是Petri Kainulainen的報價:
spring-test-mvc的核心是一個稱為MockMvc的類,可用于為使用Spring MVC實現的任何應用程序編寫測試。 我們的目標是通過使用MockMvcBuilder接口的實現來創建一個新的MockMvc對象。 MockMvcBuilders類具有四個靜態方法,可用于獲取MockMvcBuilder接口的實現。 這些方法描述如下:
- 當我們使用Java配置來配置應用程序的應用程序上下文時,必須使用ContextMockMvcBuilder注解ConfigSetup(Class…configClasses)方法。
- 當使用XML配置文件配置應用程序的應用程序上下文時,必須使用ContextMockMvcBuilder xmlConfigSetup(String…configLocations)。
- 當我們要手動配置測試的控制器和所需的MVC組件時,必須使用StandaloneMockMvcBuilder standaloneSetup(Object ... controllers)。
- 當我們已經創建了完全初始化的WebApplicationContext對象時,必須使用InitializedContextMockMvcBuilder webApplicationContextSetup(WebApplicationContext context)。
我將使用Web應用程序上下文,為此,我需要創建一個具有配置的類:
package com.sprhib.init;import org.springframework.context.annotation.*; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.view.*;@Configuration @ComponentScan("com.sprhib") @EnableWebMvc public class BaseTestConfig {@Beanpublic UrlBasedViewResolver setupViewResolver() {UrlBasedViewResolver resolver = new UrlBasedViewResolver();resolver.setPrefix("/WEB-INF/pages/");resolver.setSuffix(".jsp");resolver.setViewClass(JstlView.class);return resolver;}}最后是帶有測試的類:
package com.sprhib.test;import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;import com.sprhib.init.BaseTestConfig;@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes=BaseTestConfig.class) public class LinkControllerTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void init() {mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();}@Testpublic void testHomePage() throws Exception {mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("home"));}@Testpublic void testIndexPage() throws Exception {mockMvc.perform(get("/index.html")).andExpect(status().isOk()).andExpect(view().name("home"));}}注意,我使用了靜態導入,它們提供了諸如get(),status()等方法的用法。 @WebAppConfiguration是一個類級別的批注,用于聲明為集成測試加載的ApplicationContext應該是WebApplicationContext。 添加完所有測試內容后,查看項目結構:
在GitHub上檢查項目 。 我希望一切都清楚。 Spring測試MVC項目是測試適當應用程序的好工具。 缺少文檔和教程只是一個缺點。 在接下來的教程中,我將開發這個主題。
翻譯自: https://www.javacodegeeks.com/2013/04/spring-mvc-introduction-in-testing.html
總結
以上是生活随笔為你收集整理的Spring MVC:测试简介的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓代码颜色怎么改(安卓代码颜色)
- 下一篇: 安卓arpg单机游戏推荐(安卓arp)