概述
在日常的開發(fā)中,我們一般會(huì)定義一個(gè)service
層,用于實(shí)現(xiàn)業(yè)務(wù)邏輯,并且針對(duì)service
層會(huì)有與之對(duì)應(yīng)的齊全的覆蓋率高的單元測試。而對(duì)于controller
層,一般不怎么做單元測試,因?yàn)橹饕暮诵臉I(yè)務(wù)邏輯都在service
層里,controller
層只是做轉(zhuǎn)發(fā),調(diào)用service
層接口而已。但是還是建議使用單元測試簡單的將controller
的方法跑一下,看看轉(zhuǎn)發(fā)和數(shù)據(jù)轉(zhuǎn)換的代碼是否能正常工作。
在spring boot
里對(duì)controller
層進(jìn)行單元測試非常簡單,只需要幾個(gè)注解和一點(diǎn)點(diǎn)輔助代碼即可搞定。
依賴的包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<dependency> <groupid>org.junit.jupiter</groupid> <artifactid>junit-jupiter-api</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.junit.jupiter</groupid> <artifactid>junit-jupiter-engine</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.alibaba</groupid> <artifactid>fastjson</artifactid> </dependency> |
使用的spring boot 版本
2.0.4.release
代碼
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
|
@extendwith (springextension. class ) @springboottest (webenvironment =springboottest.webenvironment.mock,classes = testapplication. class ) @autoconfiguremockmvc public class usercontrollertest { @autowired private mockmvc mockmvc; @mockbean private userservice userservice; @test @displayname ( "測試controller方法" ) void test() throws exception { user param = new user(); param.setuserid( 1111 ); list<address> addresslist = new arraylist<>(); address address = new address(); address.setname( "我的地址" ); addresslist.add(address); param.setaddresslist(addresslist); mvcresult mvcresult = mockmvc.perform( post( "/xxx/test" ) .contenttype(mediatype.application_json) .content(json.tojsonstring(param))) .andreturn(); system.out.println(mvcresult.getresponse().getcontentasstring()); } } |
1
2
3
|
@requestmapping (value = "/xxx" , method = requestmethod.post) public object test( @requestbody (required = false )user user) throws exception { } |
如果你只是想簡單的跑一下controller
層,不想真正的去執(zhí)行service
方法的話,需要使用@mockbean
將對(duì)應(yīng)的service
類mock
掉。
1
2
|
@mockbean private userservice userservice; |
使用spring boot test
的時(shí)候,它需要一個(gè)applicationcontext
,我們可以在@springboottest
注解中使用classes
屬性來指定。
1
|
@springboottest (webenvironment =springboottest.webenvironment.mock,classes = testapplication. class ) |
testapplication
的代碼很簡單。
1
2
3
4
5
6
7
8
9
10
|
@springbootapplication public class testapplication { public static void main(string[] args){ springapplicationbuilder builder = new springapplicationbuilder(); builder.environment( new standardenvironment()); builder.sources(testapplication. class ); builder.main(testapplication. class ); builder.run(args); } } |
接下來我們只需要使用mockmvc
發(fā)送post
請求即可。如果controller
層的post
方法是帶@requestbody
注解的,可以先將入?yún)?duì)象轉(zhuǎn)換成json
字符串。這里使用的是fastjson
。
1
|
json.tojsonstring(param) |
經(jīng)過測試,如上代碼能正常工作。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,謝謝大家對(duì)服務(wù)器之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
原文鏈接:https://blog.csdn.net/linsongbin1/article/details/83574619