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

场景:由于程序中从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中操作如下

.解决 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.操作如上上图。

接下来就可以测试了。

  • 7
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

iamlzjoco

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值