一、Springboot整合测试
1、概述
对于简单易懂的小项目而言,可以不适用单元测试对平时开发没有什么影响,但是对于大型项目,单纯的依赖 “手点功能测试”, 那简直就是灾难,Springboot如何测试?
Springboot提供一系列测试工具集以及测试注解供我们测试。
- spring-boot-test提供核心测试能力
- spring-boot-test-autoconfigure提供测试的一些自动配置
- 我们只需要导入spring-boot-starter-test即可整合测试
- spring-boot-starter-test默认集成了如下库供我们测试使用
- JUnit
- Spring Test
- AssertJ
- Hamcrest
- Mockito
- JSONassert
- JsonPath
2、基本使用
创建项目引入依赖
<!--springboot程序测试依赖,如果是自动创建项目默认添加--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <!--当前依赖只参与测试范围,不参与打包--> </dependency>
项目结构
注:主程序和主测试类所属的包一样
测试使用
@SpringBootTest//标注次注解说明可以测试spring容器中的所有组件的功能 class SpringboottestApplicationTests { @Autowired UserMapper userMapper; @Test void contextLoads() { List<User> list = userMapper.selectBySql("select * from user "); System.out.println(list.toString()); } }
3、测试注解
下面是junit5测试相关的注解