工作中经常需要进行某一小段代码的测试,来保证代码的正确性。测试普通的代码还好说,如果要连接Redis,测试一些RedisTemplate读取出来的数据类型是否可以进行转换等,则比较麻烦。
我一般测试代码、请求测试接口都是创建一个单独的专门用于测试的SpringBoot项目,这样会方便很多。
进入正题:
首先第一步,测试自己封装的RedisUtils或者RedisTemplate,我们需要在项目中引入Spring的Redis相关依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
然后配置对Redis进行配置,配置一个RedisConfig配置类:
@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
jackson2JsonRedisSerializer.setObjectMapper(om);
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
template.setKeySerializer(stringRedisSerializer);
template.setHashKeySerializer(stringRedisSerializer);
template.setValueSerializer(jackson2JsonRedisSerializer);
template.setHashValueSerializer(jackson2JsonRedisSerializer);
template.afterPropertiesSet();
return template;
}
}
写一个RedisUtils工具类:
@Component
public class RedisUtils {
@Resource
public RedisTemplate<String, Object> redisTemplate;
@Resource
public StringRedisTemplate stringRedisTemplate;
/**
* 添加一个key,无过期时间
*
* @param key
* @param value
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 添加value为字符串的数据
*
* @param key
* @param value
*/
public void setString(String key, String value) {
stringRedisTemplate.opsForValue().set(key, value);
}
/**
* 添加value为字符串的数据
*
* @param key
* @param value
*/
public void setString(String key, String value, long time, TimeUnit timeUnit) {
stringRedisTemplate.opsForValue().set(key, value, time, timeUnit);
}
/**
* 获取value为字符串的数据
*
* @param key
* @return
*/
public String getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
/**
* 添加一个key,并设置过期时间
*
* @param key
* @param value
* @param time
* @param timeUnit
*/
public void set(String key, Object value, long time, TimeUnit timeUnit) {
redisTemplate.opsForValue().set(key, value, time, timeUnit);
}
/**
* get redis value
*
* @param key
* @return
*/
public Object get(String key) {
return redisTemplate.opsForValue().get(key);
}
/**
* 设置key的过期时间
*
* @param key
* @param time
* @param timeUnit
*/
public Boolean expire(String key, long time, TimeUnit timeUnit) {
return redisTemplate.expire(key, time, timeUnit);
}
/**
* 删除key
*
* @param key
*/
public Boolean delete(String key) {
return redisTemplate.delete(key);
}
/**
* 判断key是否存在
*
* @param key
* @return
*/
public Boolean hasKey(String key) {
return redisTemplate.hasKey(key);
}
/**
* 设置hash
*/
public void setHash(String key, Map<String, Object> map) {
redisTemplate.opsForHash().putAll(key, map);
}
/**
* 设置hash
*/
public void setHash(String key, String hashKey, Object value) {
redisTemplate.opsForHash().put(key, hashKey, value);
}
/**
* 判断hash中是否存在某个key
*/
public Boolean hasHash(String key, String hashKey) {
return redisTemplate.opsForHash().hasKey(key, hashKey);
}
/**
* 获取hash
*/
public Map<Object, Object> getHash(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* 获取hash某个key的值
*/
public Object getHash(String key, String hashKey) {
return redisTemplate.opsForHash().get(key, hashKey);
}
/**
* 删除hash中的某个key
*/
public Object deleteHash(String key, String hashKey) {
return redisTemplate.opsForHash().delete(key, hashKey);
}
/**
* increment
*
* @param key
* @return
*/
public Long increment(String key) {
return redisTemplate.opsForValue().increment(key);
}
/**
* increment
*
* @param key
* @param delta
* @return
*/
public Long increment(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* decrement
*
* @param key
* @return
*/
public Long decrement(String key) {
return redisTemplate.opsForValue().decrement(key);
}
/**
* decrement
*
* @param key
* @param delta
* @return
*/
public Long decrement(String key, long delta) {
return redisTemplate.opsForValue().decrement(key, delta);
}
/**
* list right push
* @param key
* @param value
*/
public void listPush(String key, String value) {
stringRedisTemplate.opsForList().rightPush(key, value);
}
/**
* list left pop string
* @param key
* @return
*/
public String listPop(String key) {
return stringRedisTemplate.opsForList().leftPop(key);
}
public List<String> range(String key) {
return stringRedisTemplate.opsForList().range(key, 0, -1);
}
public Set<String> keys(String keyPrefix) {
return redisTemplate.keys(keyPrefix + "*");
}
}
然后在test路径下创建测试类,就可以直接测试了:
@SpringBootTest
class TestApplicationTests {
@Resource
private RedisUtils redisUtils;
@Resource
private RedisTemplate<String, Object> redisTemplate;
@Test
void redisTest() {
redisUtils.set("test_key", "test_value");
redisUtils.get("test_key");
redisTemplate.opsForValue().set("test_key", "test_value");
redisTemplate.opsForValue().get("test_key");
}
}
application.yml中并没有配置Redis连接信息,默认连接信息是下面这个样子的:
spring:
redis:
database: 0
host: 127.0.0.1
port: 6379
password:
这样我们就可以随心所欲地测试Redis了,舒服。
网友评论