场景:由于程序中从redis中获取到的值不正确,需要一个测试类测试下根据key从redis中获取到的值具体是什么。所以有了下面的代码

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest(classes = WebApplication.class)
public class TestDemo {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Test
    public void test(){
        redisTemplate.opsForValue().set("key","11");
        System.out.println(redisTemplate.opsForValue().get("key"));
    }
}

但是发现代码中两个注解爆红,idea提示需要添加依赖到classpath中。

解决@RunWith爆红

1.在pom.xml中添加依赖

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
</dependency>

2.在file-->project structure中操作如下

springboot集成测试时@RunWith和@SpringBootTest爆红不能测试

.解决 SpringRunner.class爆红:

 1.添加依赖

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>5.1.11.RELEASE</version>
</dependency>

2.操作如上图

解决@SpringBootTest爆红

1.添加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <version>2.1.10.RELEASE</version>
    <!--<scope>test</scope>-->
</dependency>

 2.操作如上上图。

接下来就可以测试了。

相关文章: