test(cache): 集成测试 set/get/exists/delete 闭环 + Redis 探测脚手架
This commit is contained in:
59
src/test/java/com/njcn/middle/cache/integration/CacheRoundTripTest.java
vendored
Normal file
59
src/test/java/com/njcn/middle/cache/integration/CacheRoundTripTest.java
vendored
Normal file
@@ -0,0 +1,59 @@
|
||||
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。
|
||||
*
|
||||
* <p>连接默认 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;
|
||||
}
|
||||
}
|
||||
24
src/test/java/com/njcn/middle/cache/integration/RedisAvailableCondition.java
vendored
Normal file
24
src/test/java/com/njcn/middle/cache/integration/RedisAvailableCondition.java
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
package com.njcn.middle.cache.integration;
|
||||
|
||||
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
|
||||
import org.junit.jupiter.api.extension.ExecutionCondition;
|
||||
import org.junit.jupiter.api.extension.ExtensionContext;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
/** Redis 不可达时禁用整个测试类(skipped,非 failed),保证无 Redis 环境 mvn test 仍全绿。 */
|
||||
public class RedisAvailableCondition implements ExecutionCondition {
|
||||
@Override
|
||||
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
|
||||
String host = System.getProperty("spring.redis.host", "127.0.0.1");
|
||||
int port = Integer.parseInt(System.getProperty("spring.redis.port", "6379"));
|
||||
try (Socket s = new Socket()) {
|
||||
s.connect(new InetSocketAddress(host, port), 500);
|
||||
return ConditionEvaluationResult.enabled("redis reachable at " + host + ":" + port);
|
||||
} catch (Exception e) {
|
||||
return ConditionEvaluationResult.disabled(
|
||||
"no redis at " + host + ":" + port + ", skip integration test");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user