一、引入mockmvc依赖
使用mockmvc,必须要引入依赖
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency>
二、具体演示
1、get请求(单个参数)
测试类代码
@extendwith(mockitoextension.class) class appcontrollertest { private mockmvc mockmvc; @mock private idb2patrolresultservice idb2patrolresultservice; @injectmocks private db2patrolresultcontroller db2patrolresultcontroller; @beforeeach void setup() { this.mockmvc = mockmvcbuilders.standalonesetup(db2patrolresultcontroller).build(); } @test void testget() throws exception { this.mockmvc.perform(mockmvcrequestbuilders.get("/dbm/patrolresult/trigger/{itemcode}","121")) .andexpect(status().isok()) .anddo(mockmvcresulthandlers.print()) .andreturn(); } }
controller类代码
@getmapping("/trigger/{itemcode}") public r<void> execschedule(@pathvariable("itemcode") string itemcode) { return patrolresultservice.execschedule(itemcode); }
2、get请求(多个参数)
测试类代码:
@extendwith(mockitoextension.class) class appcontrollertest { private mockmvc mockmvc; @mock private idb2patrolresultservice idb2patrolresultservice; @injectmocks private db2patrolresultcontroller db2patrolresultcontroller; @beforeeach void setup() { this.mockmvc = mockmvcbuilders.standalonesetup(db2patrolresultcontroller).build(); } @test void testpatrolresult() throws exception { patrolqueryconfig patrolqueryconfig = new patrolqueryconfig(); patrolqueryconfig.setitemlevel("0"); patrolqueryconfig.setstatustype("0"); string jsonstr = jsonutil.tojsonstr(patrolqueryconfig); this.mockmvc.perform(mockmvcrequestbuilders.get("/dbm/patrolresult/list") .param("itemlevel","1") .param("statustype","2")) .andexpect(status().isok()) .anddo(mockmvcresulthandlers.print()) .andreturn(); } }
controller类代码
@getmapping("/list") public tabledatainfo<patrolresult> getrunstatsdataaggs(patrolqueryconfig patrolqueryconfig) { list<patrolresult> patrolresults = patrolresultservice.getpatrolhisresultaggs(patrolqueryconfig); return tabledatainfo.build(patrolresults); }
3、post请求(多个参数)
测试类代码
@extendwith(mockitoextension.class) class appcontrollertest { private mockmvc mockmvc; @mock private idb2patrolresultservice idb2patrolresultservice; @injectmocks private db2patrolresultcontroller db2patrolresultcontroller; @beforeeach void setup() { this.mockmvc = mockmvcbuilders.standalonesetup(db2patrolresultcontroller).build(); } @test void testpost() throws exception { patrolqueryconfig patrolqueryconfig = new patrolqueryconfig(); patrolqueryconfig.setitemlevel("0"); patrolqueryconfig.setstatustype("0"); string jsonstr = jsonutil.tojsonstr(patrolqueryconfig); this.mockmvc.perform(mockmvcrequestbuilders.post("/dbm/patrolresult/list") .contenttype(mediatype.application_json_value) .content(jsonstr) .header(httpheaders.accept,"application/json")) .andexpect(status().isok()) .anddo(mockmvcresulthandlers.print()) .andreturn(); } }
controller类代码
@postmapping("/list") public tabledatainfo<patrolresult> getrunstatsdataaggs(@requestbody patrolqueryconfig patrolqueryconfig) { list<patrolresult> patrolresults = patrolresultservice.getpatrolhisresultaggs(patrolqueryconfig); return tabledatainfo.build(patrolresults); }
三、总结
- 使用mockmvc可以做到controller层的测试。
- 在初始化mockmvc的时候:
(1)如果想测试单个controller
mockmvc = mockmvcbuilders.standalonesetup(mockmvccontroller).build();
(2)如果想测试所有controller
mockmvc = mockmvcbuilders.webappcontextsetup(context).build();
以上就是springboot使用mockmvc测试get和post接口的示例代码的详细内容,更多关于springboot mockmvc测试接口的资料请关注代码网其它相关文章!
发表评论