Skip to content

Commit 44ff4bb

Browse files
Copilotbinarywang
andcommitted
修复 WxCpRedisConfigImpl 中的无限递归调用问题并添加单元测试
Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
1 parent 41ea34a commit 44ff4bb

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/config/impl/WxCpRedisConfigImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,7 @@ public boolean autoRefreshToken() {
468468

469469
@Override
470470
public String getWebhookKey() {
471-
return this.getWebhookKey();
471+
return this.webhookKey;
472472
}
473473

474474
@Override
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.chanjar.weixin.cp.config.impl;
2+
3+
import org.testng.Assert;
4+
import org.testng.annotations.Test;
5+
import redis.clients.jedis.JedisPool;
6+
7+
/**
8+
* WxCpRedisConfigImpl 测试类
9+
*
10+
* @author Copilot
11+
*/
12+
public class WxCpRedisConfigImplTest {
13+
14+
/**
15+
* 测试 getWebhookKey 方法不会导致无限递归
16+
* 这个测试验证了 #issue 中提到的无限递归问题已被修复
17+
*/
18+
@Test
19+
public void testGetWebhookKeyNoInfiniteRecursion() {
20+
// 创建一个 mock JedisPool(不需要真实连接)
21+
JedisPool jedisPool = new JedisPool("localhost", 6379);
22+
23+
try {
24+
WxCpRedisConfigImpl config = new WxCpRedisConfigImpl(jedisPool);
25+
26+
// 测试1: webhookKey 为 null 时应该返回 null,而不是抛出 StackOverflowError
27+
String webhookKey = config.getWebhookKey();
28+
Assert.assertNull(webhookKey, "未设置 webhookKey 时应返回 null");
29+
30+
// 测试2: 通过反射设置 webhookKey,然后验证能够正确获取
31+
// 注意:由于 WxCpRedisConfigImpl 没有提供 setWebhookKey 方法,
32+
// 我们通过反射来设置这个字段以测试 getter 的正确性
33+
try {
34+
java.lang.reflect.Field field = WxCpRedisConfigImpl.class.getDeclaredField("webhookKey");
35+
field.setAccessible(true);
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+
} catch (NoSuchFieldException | IllegalAccessException e) {
42+
Assert.fail("反射设置 webhookKey 失败: " + e.getMessage());
43+
}
44+
} finally {
45+
// 清理资源
46+
if (jedisPool != null && !jedisPool.isClosed()) {
47+
jedisPool.close();
48+
}
49+
}
50+
}
51+
}

0 commit comments

Comments
 (0)