package com.njcn.middle.cache.integration; import com.njcn.middle.cache.template.RedisCacheEnhanceTemplate; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringBootConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; /** * 集成测试:真实 Redis 下 set/get/exists/delete 闭环。Redis 不可达自动 skip。 * *
连接默认 127.0.0.1:6379;覆盖用 {@code -Dspring.redis.host=… -Dspring.redis.port=… -Dspring.redis.password=…}。 */ @ExtendWith(RedisAvailableCondition.class) @SpringBootTest(classes = CacheRoundTripTest.TestApp.class, properties = {"redis-cache.key-prefix=it-cache:"}) class CacheRoundTripTest { @Autowired private RedisCacheEnhanceTemplate cache; @Test void setGetExistsDelete() { String key = "rt:user:1"; cache.delete(key); cache.set(key, new User(1L, "alice"), 60); User got = cache.get(key, User.class); assertEquals("alice", got.getName()); assertTrue(cache.exists(key)); assertTrue(cache.delete(key)); assertNull(cache.get(key, User.class)); assertFalse(cache.exists(key)); } @SpringBootConfiguration @EnableAutoConfiguration static class TestApp { } @Data @AllArgsConstructor @NoArgsConstructor static class User { private Long id; private String name; } }