需要在测试类上写如下注解:
@RunWith(SpringRunner.class)
@SpringBootTest(classes={RedisDemoApplication.class})
如果是maven项目需在pom.xml中写如下依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>