1、在 pom.xml 文件中添加 test 启动器依赖
<!-- 自动整合spring-test、junit -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2、编写测试类
// 指定运行环境为 Spring 环境
@RunWith(SpringRunner.class)
// 如果测试类所在目录是启动类所在目录的同级子级下可以省略启动类
// @SpringBootTest(classes = {DemoApplication.class})
@SpringBootTest
public class SpringBootJunitTest {
@Test
public void test() {
System.out.println("SpringBoot整合 Junit 单元测试");
}
}
3、跑 test 测试方法
出现如下图代表整合是 OK 的了。