SpringBoot单元测试连接redis失败:
单元测试类应该加上:@RunWith(SpringJUnit4ClassRunner.class),要使用SpringBoot环境测试。
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value={"classpath:application.properties"})
@SpringBootTest()
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.redis.core.ReactiveRedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.util.DigestUtils;
import javax.annotation.Resource;
@RunWith(SpringJUnit4ClassRunner.class)
@PropertySource(value={"classpath:application.properties"})
@SpringBootTest()
public class CodeTest {
@Resource
RedisTemplate<Object, Object> redisTemplate;
@Test
public void test(){
String s = DigestUtils.md5DigestAsHex(("yt" + "3zLE").getBytes());
System.out.println(redisTemplate.opsForValue().get(s));
}
}