@RunWith(SpringRunner.class)这个注解作用是在test的时候建立spring上下文环境
当pom.xml中有
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies>
这个依赖的时候,需要spring上下文环境就需要写这个注解
当pom.xml是下面这样时
<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>
就不需要@RunWith(SpringRunner.class)这个注解了,因为已经有了spring上下文环境