在springmvc中一般的測試用例都是測試service層,今天我來演示下如何使用springmvc mock直接測試controller層代碼。
1.什么是mock測試?
mock測試就是在測試過程中,對于某些不容易構(gòu)造或者不容易獲取的對象,用一個虛擬的對象來創(chuàng)建以便測試的測試方法。
2.為什么要使用mock測試?
使用mock o bject進行測試,主要是用來模擬那些在應(yīng)用中不容易構(gòu)造(如httpservletrequest必須在servlet容器中才能構(gòu)造出來)或者比較復雜的對象(如jdbc中的resultset對象)從而使測試順利進行的工具。
3.常用注解
runwith(springjunit4classrunner.class): 表示使用spring test組件進行單元測試;
webappconfiguratio: 使用這個annotation會在跑單元測試的時候真實的啟一個web服務(wù),然后開始調(diào)用controller的rest api,待單元測試跑完之后再將web服務(wù)停掉;
contextconfiguration: 指定bean的配置文件信息,可以有多種方式,這個例子使用的是文件路徑形式,如果有多個配置文件,可以將括號中的信息配置為一個字符串數(shù)組來表示;
4.安裝測試環(huán)境
spring mvc測試框架提供了兩種方式,獨立安裝和集成web環(huán)境測試(此種方式并不會集成真正的web環(huán)境,而是通過相應(yīng)的mock api進行模擬測試,無須啟動服務(wù)器)。
獨立安裝測試方式
mockmvcbuilders.standalonesetup(object... controllers):通過參數(shù)指定一組控制器,這樣就不需要從上下文獲取了;
主要是兩個步驟:
(1)首先自己創(chuàng)建相應(yīng)的控制器,注入相應(yīng)的依賴
(2)通過mockmvcbuilders.standalonesetup模擬一個mvc測試環(huán)境,通過build得到一個mockmvc
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
package com.xfs.test; import org.junit. assert ; import org.junit.before; import org.junit.test; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresulthandlers; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import com.alibaba.fastjson.json; import com.alibaba.fastjson.jsonobject; import com.xfs.web.controller.apicontroller; /** * 獨立安裝測試方式 springmvc mock測試 * * @author admin * * 2017年11月23日 上午10:39:49 */ public class testapione { private mockmvc mockmvc; @before public void setup() { apicontroller apicontroller = new apicontroller(); mockmvc = mockmvcbuilders.standalonesetup(apicontroller).build(); } @test public void testgetsequence() { try { mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.post( "/api/getsequence" )) .andexpect(mockmvcresultmatchers.status().is( 200 )) .anddo(mockmvcresulthandlers.print()) .andreturn(); int status = mvcresult.getresponse().getstatus(); system.out.println( "請求狀態(tài)碼:" + status); string result = mvcresult.getresponse().getcontentasstring(); system.out.println( "接口返回結(jié)果:" + result); jsonobject resultobj = json.parseobject(result); // 判斷接口返回json中success字段是否為true assert .asserttrue(resultobj.getbooleanvalue( "success" )); } catch (exception e) { e.printstacktrace(); } } } |
請求結(jié)果如下:
集成web環(huán)境方式
mockmvcbuilders.webappcontextsetup(webapplicationcontext context):指定webapplicationcontext,將會從該上下文獲取相應(yīng)的控制器并得到相應(yīng)的mockmvc;
主要是三個步驟:
(1)@webappconfiguration:測試環(huán)境使用,用來表示測試環(huán)境使用的applicationcontext將是webapplicationcontext類型的;value指定web應(yīng)用的根
(2)通過@autowired webapplicationcontext wac:注入web環(huán)境的applicationcontext容器
(3)然后通過mockmvcbuilders.webappcontextsetup(wac).build()創(chuàng)建一個mockmvc進行測試
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
package com.xfs.test; import org.junit. assert ; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.springframework.beans.factory.annotation.autowired; import org.springframework.mock.web.mockhttpsession; import org.springframework.test.context.contextconfiguration; import org.springframework.test.context.junit4.abstractjunit4springcontexttests; import org.springframework.test.context.web.webappconfiguration; import org.springframework.test.web.servlet.mockmvc; import org.springframework.test.web.servlet.mvcresult; import org.springframework.test.web.servlet.request.mockmvcrequestbuilders; import org.springframework.test.web.servlet.result.mockmvcresulthandlers; import org.springframework.test.web.servlet.result.mockmvcresultmatchers; import org.springframework.test.web.servlet.setup.mockmvcbuilders; import org.springframework.web.context.webapplicationcontext; import com.alibaba.fastjson.json; import com.alibaba.fastjson.jsonobject; /** * 集成web環(huán)境方式 springmvc mock測試 * * @author admin * * 2017年11月23日 上午11:12:43 */ @runwith (junit4classrunner. class ) @webappconfiguration @contextconfiguration (locations = { "classpath*:spring/*.xml" }) public class testapitwo extends abstractjunit4springcontexttests { @autowired public webapplicationcontext wac; public mockmvc mockmvc; public mockhttpsession session; @before public void before() throws exception { mockmvc = mockmvcbuilders.webappcontextsetup(wac).build(); } @test public void testgetsequence() { try { mvcresult mvcresult = mockmvc.perform(mockmvcrequestbuilders.post( "/api/getsequence" )) .andexpect(mockmvcresultmatchers.status().is( 200 )) .anddo(mockmvcresulthandlers.print()) .andreturn(); int status = mvcresult.getresponse().getstatus(); system.out.println( "請求狀態(tài)碼:" + status); string result = mvcresult.getresponse().getcontentasstring(); system.out.println( "接口返回結(jié)果:" + result); jsonobject resultobj = json.parseobject(result); // 判斷接口返回json中success字段是否為true assert .asserttrue(resultobj.getbooleanvalue( "success" )); } catch (exception e) { e.printstacktrace(); } } } |
運行結(jié)果和上面獨立測試時候一樣。
總結(jié):
整個過程:
1、mockmvc.perform執(zhí)行一個請求;
2、mockmvcrequestbuilders.get("/user/1")構(gòu)造一個請求
3、resultactions.andexpect添加執(zhí)行完成后的斷言
4、resultactions.anddo添加一個結(jié)果處理器,表示要對結(jié)果做點什么事情,比如此處使用mockmvcresulthandlers.print()輸出整個響應(yīng)結(jié)果信息。
5、resultactions.andreturn表示執(zhí)行完成后返回相應(yīng)的結(jié)果。
整個測試過程非常有規(guī)律:
1、準備測試環(huán)境
2、通過mockmvc執(zhí)行請求
3、添加驗證斷言
4、添加結(jié)果處理器
5、得到mvcresult進行自定義斷言/進行下一步的異步請求
6、卸載測試環(huán)境
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.linuxidc.com/Linux/2017-12/149851.htm