springbootTest单元测试说明

目录

    当pom.xml中引入的是

    <dependencies>
          <dependency>
              <groupId>org.springframework.boot</groupId>
              <artifactId>spring-boot-starter-test</artifactId>
              <scope>test</scope>
              <exclusions>
                  <exclusion>
                      <groupId>org.junit.vintage</groupId>
                      <artifactId>junit-vintage-engine</artifactId>
                  </exclusion>
              </exclusions>
          </dependency>
    </dependencies>
    

    在单元测试中的测试类不需要写注解@RunWith(SpringRunner.class),
    注解@SpringBootTest会帮我们引入@RunWith这个注解

    当引入的测试包是这个依赖

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    

    测试类需要写@RunWith(SpringRunner.class)注解

    如:

    @SpringBootTest
    @RunWith(SpringRunner.class)
    public class RedisTemplateTests {
    }
    

    注解@RunWith(SpringRunner.class)的使用场景是当测试类中需要使用spring容器中组件时,需要根据使用的测试包不同(上面两种方式)引入这个注解

    posted @ 2022-09-12 17:52  Lz_蚂蚱  阅读(74)  评论(0)    收藏  举报