说明

单元测试的基本用法,最主要就是@runwith(springrunner.class)记不住

代码

引入依赖

  1. <parent>
  2. <groupId>org.springframework.boot</groupId>
  3. <artifactId>spring-boot-starter-parent</artifactId>
  4. <version>2.3.2.RELEASE</version>
  5. <relativePath/>
  6. </parent>
  7. <dependency>
  8. <groupId>org.springframework.boot</groupId>
  9. <artifactId>spring-boot-starter</artifactId>
  10. </dependency>
  11. <dependency>
  12. <groupId>org.springframework.boot</groupId>
  13. <artifactId>spring-boot-starter-test</artifactId>
  14. <scope>test</scope>
  15. </dependency>

在SpringBoot中的代码

  1. @SpringBootTest
  2. @RunWith(SpringRunner.class)
  3. class CommunitystudyApplicationTests {
  4. @Autowired
  5. UserService userService;
  6. @Test
  7. void contextLoads() {
  8. System.out.println("测试代码");
  9. }
  10. }

注意

1.重要的依赖就是spring-boot-starter-test

2.重要的注解:
@SpringBootTest
@RunWith(SpringRunner.class)