SpringBoot---单元测试
1、pom.xml中添加spring-boot-starter-test测试启动器依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、在test目录下新建对应模块的文件夹,并在该文件夹中新建java测试类,
注意:单元测试类需要加上@RunWith(SpringRunner.class)和 @Spring BootTes注解,方法需要加上@Test注解
点击编写的测试方法左侧的绿色箭头,再点击Run ‘方法名()’运行测试方法。 当方法全部打勾时说明测试通过。
注解 |
功能说明 |
@RunWith(SpringRunner.class) |
加载Spring上下文 |
@SpringBootTest |
开启SpringBoot的测试 |
@Autowired |
注入Bean对象 |
@Test |
测试方法 |