依赖配置
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
类上注解
@RunWith(SpringRunner.class) //测试启动器,并加载Spring Boot测试注解
@SpringBootTest // 标记为Spring Boot单元测试类,并加载项目的ApplicationContext上下文环境
public class SpringbootDemoApplicationTests {
@Autowired
private DemoController demoController;
//自动创建的单元测试方法实例
@Test
public void contextLoads() {
String demo = demoController.demo();
System.out.println(demo);
}
}
文章内容输出来源:拉勾教育Java高薪训练营

本文介绍如何使用Spring Boot进行单元测试,包括配置依赖、使用@RunWith和@SpringBootTest注解启动测试环境,以及通过@Autowired注入控制器并执行测试方法。
516

被折叠的 条评论
为什么被折叠?



