1
0
mirror of synced 2026-02-25 15:49:36 +08:00

fix: 修复 PR review comments

1. 为接口方法 switchover(String mchId) 和 switchoverTo(String mchId) 添加 default 实现,保证向后兼容
2. 将手动测试中的 Java assert 替换为显式 verify() 方法,避免断言不生效的问题
This commit is contained in:
Binary Wang
2026-01-16 17:23:47 +08:00
parent 3cdd53d765
commit a2ac1d15d4
2 changed files with 32 additions and 16 deletions

View File

@@ -86,7 +86,9 @@ public interface WxPayService {
* @param mchId 商户标识 * @param mchId 商户标识
* @return 切换是否成功如果找不到匹配的配置则返回false * @return 切换是否成功如果找不到匹配的配置则返回false
*/ */
boolean switchover(String mchId); default boolean switchover(String mchId) {
return false;
}
/** /**
* 进行相应的商户切换. * 进行相应的商户切换.
@@ -104,9 +106,11 @@ public interface WxPayService {
* *
* @param mchId 商户标识 * @param mchId 商户标识
* @return 切换成功,则返回当前对象,方便链式调用 * @return 切换成功,则返回当前对象,方便链式调用
* @throws WxRuntimeException 如果找不到匹配的配置 * @throws me.chanjar.weixin.common.error.WxRuntimeException 如果找不到匹配的配置
*/ */
WxPayService switchoverTo(String mchId); default WxPayService switchoverTo(String mchId) {
throw new me.chanjar.weixin.common.error.WxRuntimeException("子类需要实现此方法");
}
/** /**
* 发送post请求得到响应字节数组. * 发送post请求得到响应字节数组.

View File

@@ -47,8 +47,8 @@ public class MultiAppIdSwitchoverManualTest {
boolean success = payService.switchover(testMchId, testAppId1); boolean success = payService.switchover(testMchId, testAppId1);
System.out.println("切换结果: " + success); System.out.println("切换结果: " + success);
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey()); System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
assert success : "切换应该成功"; verify(success, "切换应该成功");
assert testAppId1.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId1; verify(testAppId1.equals(payService.getConfig().getAppId()), "AppId应该是 " + testAppId1);
System.out.println("✓ 测试1通过\n"); System.out.println("✓ 测试1通过\n");
// 测试2: 仅使用 mchId 切换 // 测试2: 仅使用 mchId 切换
@@ -56,8 +56,8 @@ public class MultiAppIdSwitchoverManualTest {
success = payService.switchover(testMchId); success = payService.switchover(testMchId);
System.out.println("切换结果: " + success); System.out.println("切换结果: " + success);
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey()); System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
assert success : "仅使用mchId切换应该成功"; verify(success, "仅使用mchId切换应该成功");
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId; verify(testMchId.equals(payService.getConfig().getMchId()), "MchId应该是 " + testMchId);
System.out.println("✓ 测试2通过\n"); System.out.println("✓ 测试2通过\n");
// 测试3: 使用 switchoverTo 链式调用(精确匹配) // 测试3: 使用 switchoverTo 链式调用(精确匹配)
@@ -65,8 +65,8 @@ public class MultiAppIdSwitchoverManualTest {
WxPayService result = payService.switchoverTo(testMchId, testAppId2); WxPayService result = payService.switchoverTo(testMchId, testAppId2);
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例")); System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey()); System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
assert result == payService : "应该返回同一实例"; verify(result == payService, "应该返回同一实例");
assert testAppId2.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId2; verify(testAppId2.equals(payService.getConfig().getAppId()), "AppId应该是 " + testAppId2);
System.out.println("✓ 测试3通过\n"); System.out.println("✓ 测试3通过\n");
// 测试4: 使用 switchoverTo 链式调用仅mchId // 测试4: 使用 switchoverTo 链式调用仅mchId
@@ -74,22 +74,22 @@ public class MultiAppIdSwitchoverManualTest {
result = payService.switchoverTo(testMchId); result = payService.switchoverTo(testMchId);
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例")); System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey()); System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
assert result == payService : "应该返回同一实例"; verify(result == payService, "应该返回同一实例");
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId; verify(testMchId.equals(payService.getConfig().getMchId()), "MchId应该是 " + testMchId);
System.out.println("✓ 测试4通过\n"); System.out.println("✓ 测试4通过\n");
// 测试5: 切换到不存在的商户号 // 测试5: 切换到不存在的商户号
System.out.println("=== 测试5: 切换到不存在的商户号 ==="); System.out.println("=== 测试5: 切换到不存在的商户号 ===");
success = payService.switchover("nonexistent_mch_id"); success = payService.switchover("nonexistent_mch_id");
System.out.println("切换结果: " + success); System.out.println("切换结果: " + success);
assert !success : "切换到不存在的商户号应该失败"; verify(!success, "切换到不存在的商户号应该失败");
System.out.println("✓ 测试5通过\n"); System.out.println("✓ 测试5通过\n");
// 测试6: 切换到不存在的 appId // 测试6: 切换到不存在的 appId
System.out.println("=== 测试6: 切换到不存在的 appId ==="); System.out.println("=== 测试6: 切换到不存在的 appId ===");
success = payService.switchover(testMchId, "wx9999999999999999"); success = payService.switchover(testMchId, "wx9999999999999999");
System.out.println("切换结果: " + success); System.out.println("切换结果: " + success);
assert !success : "切换到不存在的appId应该失败"; verify(!success, "切换到不存在的appId应该失败");
System.out.println("✓ 测试6通过\n"); System.out.println("✓ 测试6通过\n");
// 测试7: 添加新配置后切换 // 测试7: 添加新配置后切换
@@ -100,16 +100,28 @@ public class MultiAppIdSwitchoverManualTest {
newConfig.setAppId(newAppId); newConfig.setAppId(newAppId);
newConfig.setMchKey("test_key_4"); newConfig.setMchKey("test_key_4");
payService.addConfig(testMchId, newAppId, newConfig); payService.addConfig(testMchId, newAppId, newConfig);
success = payService.switchover(testMchId, newAppId); success = payService.switchover(testMchId, newAppId);
System.out.println("切换结果: " + success); System.out.println("切换结果: " + success);
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey()); System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
assert success : "切换到新添加的配置应该成功"; verify(success, "切换到新添加的配置应该成功");
assert newAppId.equals(payService.getConfig().getAppId()) : "AppId应该是 " + newAppId; verify(newAppId.equals(payService.getConfig().getAppId()), "AppId应该是 " + newAppId);
System.out.println("✓ 测试7通过\n"); System.out.println("✓ 测试7通过\n");
System.out.println("=================="); System.out.println("==================");
System.out.println("所有测试通过! ✓"); System.out.println("所有测试通过! ✓");
System.out.println("=================="); System.out.println("==================");
} }
/**
* 验证条件是否为真,如果为假则抛出异常
*
* @param condition 待验证的条件
* @param message 验证失败时的错误信息
*/
private static void verify(boolean condition, String message) {
if (!condition) {
throw new RuntimeException("验证失败: " + message);
}
}
} }