SpringBoot Controller Post接口单元测试示例

2025-05-29 0 56

概述

在日常的开发中,我们一般会定义一个service层,用于实现业务逻辑,并且针对service层会有与之对应的齐全的覆盖率高的单元测试。而对于controller层,一般不怎么做单元测试,因为主要的核心业务逻辑都在service层里,controller层只是做转发,调用service接口而已。但是还是建议使用单元测试简单的将controller的方法跑一下,看看转发和数据转换的代码是否能正常工作。

spring boot里对controller层进行单元测试非常简单,只需要几个注解和一点点辅助代码即可搞定。

依赖的包

?

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层,不想真正的去执行service方法的话,需要使用@mockbean将对应的servicemock掉。

?

1

2
@mockbean

private userservice userservice;

使用spring boot test的时候,它需要一个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发送post请求即可。如果controller层的post方法是带@requestbody注解的,可以先将入参对象转换成json字符串。这里使用的是fastjson

?

1
json.tojsonstring(param)

经过测试,如上代码能正常工作。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对快网idc的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/linsongbin1/article/details/83574619

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot Controller Post接口单元测试示例 https://www.kuaiidc.com/110842.html

相关文章

发表评论
暂无评论