package org.example.config; 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.test.context.TestPropertySource; import static org.junit.jupiter.api.Assertions.*; /** * 单独测试 Redis 连接 * 禁用 Milvus 以避免启动失败 */ @SpringBootTest @TestPropertySource(properties = { "spring.autoconfigure.exclude=org.example.config.MilvusConfig" }) class RedisConnectionTest { @Autowired(required = false) private RedisTemplate redisTemplate; @Test void testRedisConnection() { assertNotNull(redisTemplate, "RedisTemplate should be configured"); try { String testKey = "test:connection:" + System.currentTimeMillis(); String testValue = "test-value"; // 测试写入 redisTemplate.opsForValue().set(testKey, testValue); System.out.println("✓ Redis 写入成功"); // 测试读取 Object result = redisTemplate.opsForValue().get(testKey); assertEquals(testValue, result, "Redis read should return the written value"); System.out.println("✓ Redis 读取成功"); // 测试删除 redisTemplate.delete(testKey); Object deleted = redisTemplate.opsForValue().get(testKey); assertNull(deleted, "Deleted key should return null"); System.out.println("✓ Redis 删除成功"); System.out.println("\n✓ Redis 连接测试通过!"); System.out.println(" 服务器: 119.29.78.52:6379"); System.out.println(" 读写删操作正常"); } catch (Exception e) { fail("Redis 连接失败: " + e.getMessage()); } } }