0
点赞
收藏
分享

微信扫一扫

Springboot项目单元测试(test类+postman)

janedaring 2022-04-13 阅读 104

通过test类进行单元测试

在pom文件中test依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

在指定的test/java目录下进行编写,然后在里面创建测试类

在这里插入图片描述

单元测试类

添加注释@SpringBootTest(classes = xxxxx.class)

(xxxxx:这个表示的是启动类的名字)
@SpringBootTest注解是Springboot提供的注解,主要使得junit可以在springboot环境进行测试
classes参数是可以用来指定springboot项目的启动类
@SpringBootTest是作用类上,它主要是用来启动本地的SpringBoot环境(也就是用来启动springboot项目)

在测试的方法上面添加注释@test进行测试

(想测试哪个类就用@Autowired进行注入)
在这里插入图片描述

通过postman进行测试接口

将springboot项目启动,观察项目的端口号:(可以看到端口号为8888)

在这里插入图片描述
然后将要测试的接口在postman进行测试,ip地址为localhost
以一个添加好友的接口为例,进行实践操作
controller类:
在这里插入图片描述

postman测试:输入:localhost:8888/friend

在这里插入图片描述

举报

相关推荐

0 条评论