package com.superbiz.agent.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 javax.sql.DataSource; import java.sql.Connection; import static org.junit.jupiter.api.Assertions.*; /** * 测试 MySQL 和 Redis 连接配置 */ @SpringBootTest class ConnectionConfigTest { @Autowired(required = false) private DataSource dataSource; @Autowired(required = false) private RedisTemplate redisTemplate; @Test void testMySQLConnection() { assertNotNull(dataSource, "DataSource should be configured"); try (Connection connection = dataSource.getConnection()) { assertNotNull(connection, "Connection should not be null"); assertFalse(connection.isClosed(), "Connection should be open"); String catalog = connection.getCatalog(); System.out.println("✓ MySQL 连接成功!数据库: " + catalog); assertEquals("superbiz_agent", catalog, "Database name should be superbiz_agent"); } catch (Exception e) { fail("MySQL 连接失败: " + e.getMessage()); } } @Test void testRedisConnection() { assertNotNull(redisTemplate, "RedisTemplate should be configured"); try { // 测试 PING String testKey = "test:connection:" + System.currentTimeMillis(); String testValue = "test-value"; redisTemplate.opsForValue().set(testKey, testValue); Object result = redisTemplate.opsForValue().get(testKey); assertEquals(testValue, result, "Redis read/write should work"); System.out.println("✓ Redis 连接成功!读写正常"); // 清理测试数据 redisTemplate.delete(testKey); } catch (Exception e) { fail("Redis 连接失败: " + e.getMessage()); } } }