SpringBoot2.4以上版本
导入junit依赖
<dependency>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
在测试类后面alt+ins

选择Junit4和要Test的类

添加注解(classes中要写启动类名字)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = ApiApplication.class)
public class UserServiceTest {
@Autowired
private UserService userService;
@Test
public void userRegist() {
}
}
本文介绍了如何在Spring Boot 2.4及以上版本中使用Junit5和SpringRunner进行单元测试,包括添加相关依赖、配置测试类和使用@SpringBootTest注解。通过实例演示了如何在测试类中注入UserService并进行userRegist方法的测试。
4877

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



