日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring 4 MVC 单元测试例子

發布時間:2023/12/10 javascript 48 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring 4 MVC 单元测试例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

首先,要有一個Spring MVC項目,不會的話,點這里看教程。

加入 maven 依賴:

<dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version></dependency><!-- spring test --><!-- 提供測試支持--> <dependency><groupId>org.springframework</groupId><artifactId>spring-test</artifactId><version>4.2.4.RELEASE</version></dependency>

被測試的類:SayHelloController。這個類不用改動,只是貼出來表明,通過 /SayHello/getAnswer 就可以訪問到 helloWorld() 方法

@Controller @RequestMapping("/SayHello") public class SayHelloController { @RequestMapping( path = "/getAnswer" , method = RequestMethod.GET)public String helloWorld() {return "redirect:/answer.jsp";}}

測試類:SayHelloControllerTest。按 ctrl+o 導入依賴。

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:spring-servlet.xml"}) public class SayHelloControllerTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void setup() {this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();}@Testpublic void testHelloWorld() throws Exception {mockMvc.perform(get("/SayHello/getAnswer")).andDo(print()).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/answer.jsp"));}}

注意: 如果你的spring 和spring mvc 配置文件放在 /WEB-INF/ 目錄下,那么選中 /WEB-INF/ 目錄,“右鍵”–“build path”– “use as source folder” ,將兩個配置文件加入到 classes目錄下,因為 classpath:要求配置文件在classes目錄下。
上面代碼中的get方法需要靜態引入,添加引入語句如下:
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

完了,要做的只有這么多:增加依賴項,然后寫一個測試類。可以通過 run as junit test 來看下效果。

上面的代碼,可以看成一個模板+一個測試方法,模板如下:

@RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(locations={"classpath:applicationContext.xml","classpath:spring-servlet.xml"}) public class SayHelloControllerTest {@Autowiredprivate WebApplicationContext wac;private MockMvc mockMvc;@Beforepublic void setup() {this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();}// 下面寫測試方法}

這個模板,只要改變配置文件的位置,其它不用動。模板代碼的作用就是創建模擬的測試環境。

測試方法如下:

@Testpublic void testHelloWorld() throws Exception {mockMvc.perform(get("/SayHello/getAnswer")).andDo(print()).andExpect(status().is3xxRedirection()).andExpect(redirectedUrl("/answer.jsp"));}

上面這段代碼,用 perform(get("/SayHello/getAnswer")) 發送一個請求,這個請求和真正的請求一樣,會經過DispatcherServlet,然后調用被測試類SayHelloController 的helloWorld()方法。

.andDo(print())是當請求執行完后,執行打印所有相關信息動作。print()是一個靜態方法,來自MockMvcResultHandlers 。

.andExpect(status().is3xxRedirection())。.andExpect( 預期)判斷實際響應與預期是否相等。比如這里,我斷言發送/SayHello/getAnswer請求后,會返回一個響應,響應狀態為3XX重定向。如果測試中,實際返回的是3XX重定向,這個方法不會出現問題,但如果返回的不是3XX,那么這個方法就會拋出異常,我們就知道被測試的類可能某個地方出現問題了。


項目代碼

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的Spring 4 MVC 单元测试例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。