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() {
}
}