package edu.whut.smilepicturebackend; import lombok.RequiredArgsConstructor; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.data.redis.core.ValueOperations; import static org.junit.jupiter.api.Assertions.*; @SpringBootTest public class RedisStringTest { @Autowired private StringRedisTemplate stringRedisTemplate; @Test public void testRedisStringOperations() { // 获取操作对象 ValueOperations valueOps = stringRedisTemplate.opsForValue(); // Key 和 Value String key = "testKey"; String value = "testValue"; // 1. 测试新增或更新操作 valueOps.set(key, value); String storedValue = valueOps.get(key); assertEquals(value, storedValue, "存储的值与预期不一致"); // 2. 测试修改操作 String updatedValue = "updatedValue"; valueOps.set(key, updatedValue); storedValue = valueOps.get(key); assertEquals(updatedValue, storedValue, "更新后的值与预期不一致"); // 3. 测试查询操作 storedValue = valueOps.get(key); assertNotNull(storedValue, "查询的值为空"); assertEquals(updatedValue, storedValue, "查询的值与预期不一致"); // // 4. 测试删除操作 // redisTemplate.delete(key); // storedValue = valueOps.get(key); // assertNull(storedValue, "删除后的值不为空"); } }