I am trying to test redis server works in a test code.So I made a Redis test code like below
@Testvoid saveToken(){ MofMember mofM = new MofMember("123", "123", "123@123.com", "Lee", "lelele", "male", "11/10", "1999", "123-123-123"); String token = ts.generateToken(mofM); assertNotNull(token, "this is null"); assertDoesNotThrow(() -> { ts.saveToken(token); });}
I but when I try to test I get a error below
Caused by: io.netty.channel.AbstractChannel$AnnotatedConnectException:Connection refused: no further information: localhost/127.0.0.1:6379
To connect embeded redis I made an Redis configuration like below
RedisConf class
@Configurationpublic class RedisConf {
private static final Logger logger = LoggerFactory.getLogger(RedisConf.class.getName());@Beanpublic LettuceConnectionFactory redisConnectionFactory() { return new LettuceConnectionFactory();}@Beanpublic RedisTemplate<String, String> redisTemplate() { RedisTemplate<String, String> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(redisConnectionFactory()); return redisTemplate;}
}
and then made an RedisCrud class to save token
RedisCrud.saveToken()
private final RedisTemplate<String, String> redisTemplate;private RedisConf redisConf;public RedisCrud(RedisConf redisConf){ this.redisConf = redisConf; redisTemplate = this.redisConf.redisTemplate();}public boolean saveToken(String key, String value) throws Exception{ logger.info("Key : "+key+", Value : "+value); try{ logger.info(redisTemplate.toString()); logger.info(redisTemplate.opsForValue().toString()); redisTemplate.opsForValue().set(key, value); return true; }catch(Exception e){ throw new Exception("Redis Exception occur",e); }}
and then I made an service class that will call saveToken()TokenService class
public boolean saveToken(String token) throws Exception{ JwtParser parser = Jwts.parserBuilder().setSigningKey(secretKey).build(); Claims claims = parser.parseClaimsJws(token).getBody(); String key = claims.getSubject(); try{ redis.saveToken(key, token); return true; }catch(Exception e){ throw new Exception("Saving token error",e); }}
I want to solve this error and test redis in local.How can I solve this problem??