|
| 1 | +package me.chanjar.weixin.cp.config.impl; |
| 2 | + |
| 3 | +import org.mockito.Mockito; |
| 4 | +import org.testng.Assert; |
| 5 | +import org.testng.annotations.Test; |
| 6 | +import redis.clients.jedis.JedisPool; |
| 7 | + |
| 8 | +/** |
| 9 | + * WxCpRedisConfigImpl 测试类 |
| 10 | + */ |
| 11 | +public class WxCpRedisConfigImplTest { |
| 12 | + |
| 13 | + /** |
| 14 | + * 测试 getWebhookKey 方法不会导致无限递归 |
| 15 | + * 这个测试验证了 #issue 中提到的无限递归问题已被修复 |
| 16 | + */ |
| 17 | + @Test |
| 18 | + public void testGetWebhookKeyNoInfiniteRecursion() { |
| 19 | + // 使用 Mockito 创建 mock JedisPool,避免真实连接 |
| 20 | + JedisPool jedisPool = Mockito.mock(JedisPool.class); |
| 21 | + |
| 22 | + WxCpRedisConfigImpl config = new WxCpRedisConfigImpl(jedisPool); |
| 23 | + |
| 24 | + // 测试1: webhookKey 为 null 时应该返回 null,而不是抛出 StackOverflowError |
| 25 | + String webhookKey = config.getWebhookKey(); |
| 26 | + Assert.assertNull(webhookKey, "未设置 webhookKey 时应返回 null"); |
| 27 | + |
| 28 | + // 测试2: 通过反射设置 webhookKey,然后验证能够正确获取 |
| 29 | + // 注意:由于 WxCpRedisConfigImpl 没有提供 setWebhookKey 方法, |
| 30 | + // 我们通过反射来设置这个字段以测试 getter 的正确性 |
| 31 | + try { |
| 32 | + java.lang.reflect.Field field = WxCpRedisConfigImpl.class.getDeclaredField("webhookKey"); |
| 33 | + boolean originalAccessible = field.isAccessible(); |
| 34 | + field.setAccessible(true); |
| 35 | + try { |
| 36 | + String testWebhookKey = "test-webhook-key-123"; |
| 37 | + field.set(config, testWebhookKey); |
| 38 | + |
| 39 | + String retrievedKey = config.getWebhookKey(); |
| 40 | + Assert.assertEquals(retrievedKey, testWebhookKey, "应该返回设置的 webhookKey 值"); |
| 41 | + } finally { |
| 42 | + field.setAccessible(originalAccessible); |
| 43 | + } |
| 44 | + } catch (NoSuchFieldException | IllegalAccessException e) { |
| 45 | + Assert.fail("反射设置 webhookKey 失败: " + e.getMessage()); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments