fix: 修复 PR review comments
1. 为接口方法 switchover(String mchId) 和 switchoverTo(String mchId) 添加 default 实现,保证向后兼容 2. 将手动测试中的 Java assert 替换为显式 verify() 方法,避免断言不生效的问题
This commit is contained in:
@@ -86,7 +86,9 @@ public interface WxPayService {
|
||||
* @param mchId 商户标识
|
||||
* @return 切换是否成功,如果找不到匹配的配置则返回false
|
||||
*/
|
||||
boolean switchover(String mchId);
|
||||
default boolean switchover(String mchId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 进行相应的商户切换.
|
||||
@@ -104,9 +106,11 @@ public interface WxPayService {
|
||||
*
|
||||
* @param mchId 商户标识
|
||||
* @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请求,得到响应字节数组.
|
||||
|
||||
@@ -47,8 +47,8 @@ public class MultiAppIdSwitchoverManualTest {
|
||||
boolean success = payService.switchover(testMchId, testAppId1);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "切换应该成功";
|
||||
assert testAppId1.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId1;
|
||||
verify(success, "切换应该成功");
|
||||
verify(testAppId1.equals(payService.getConfig().getAppId()), "AppId应该是 " + testAppId1);
|
||||
System.out.println("✓ 测试1通过\n");
|
||||
|
||||
// 测试2: 仅使用 mchId 切换
|
||||
@@ -56,8 +56,8 @@ public class MultiAppIdSwitchoverManualTest {
|
||||
success = payService.switchover(testMchId);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "仅使用mchId切换应该成功";
|
||||
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId;
|
||||
verify(success, "仅使用mchId切换应该成功");
|
||||
verify(testMchId.equals(payService.getConfig().getMchId()), "MchId应该是 " + testMchId);
|
||||
System.out.println("✓ 测试2通过\n");
|
||||
|
||||
// 测试3: 使用 switchoverTo 链式调用(精确匹配)
|
||||
@@ -65,8 +65,8 @@ public class MultiAppIdSwitchoverManualTest {
|
||||
WxPayService result = payService.switchoverTo(testMchId, testAppId2);
|
||||
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert result == payService : "应该返回同一实例";
|
||||
assert testAppId2.equals(payService.getConfig().getAppId()) : "AppId应该是 " + testAppId2;
|
||||
verify(result == payService, "应该返回同一实例");
|
||||
verify(testAppId2.equals(payService.getConfig().getAppId()), "AppId应该是 " + testAppId2);
|
||||
System.out.println("✓ 测试3通过\n");
|
||||
|
||||
// 测试4: 使用 switchoverTo 链式调用(仅mchId)
|
||||
@@ -74,22 +74,22 @@ public class MultiAppIdSwitchoverManualTest {
|
||||
result = payService.switchoverTo(testMchId);
|
||||
System.out.println("返回对象: " + (result == payService ? "同一实例" : "不同实例"));
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert result == payService : "应该返回同一实例";
|
||||
assert testMchId.equals(payService.getConfig().getMchId()) : "MchId应该是 " + testMchId;
|
||||
verify(result == payService, "应该返回同一实例");
|
||||
verify(testMchId.equals(payService.getConfig().getMchId()), "MchId应该是 " + testMchId);
|
||||
System.out.println("✓ 测试4通过\n");
|
||||
|
||||
// 测试5: 切换到不存在的商户号
|
||||
System.out.println("=== 测试5: 切换到不存在的商户号 ===");
|
||||
success = payService.switchover("nonexistent_mch_id");
|
||||
System.out.println("切换结果: " + success);
|
||||
assert !success : "切换到不存在的商户号应该失败";
|
||||
verify(!success, "切换到不存在的商户号应该失败");
|
||||
System.out.println("✓ 测试5通过\n");
|
||||
|
||||
// 测试6: 切换到不存在的 appId
|
||||
System.out.println("=== 测试6: 切换到不存在的 appId ===");
|
||||
success = payService.switchover(testMchId, "wx9999999999999999");
|
||||
System.out.println("切换结果: " + success);
|
||||
assert !success : "切换到不存在的appId应该失败";
|
||||
verify(!success, "切换到不存在的appId应该失败");
|
||||
System.out.println("✓ 测试6通过\n");
|
||||
|
||||
// 测试7: 添加新配置后切换
|
||||
@@ -100,16 +100,28 @@ public class MultiAppIdSwitchoverManualTest {
|
||||
newConfig.setAppId(newAppId);
|
||||
newConfig.setMchKey("test_key_4");
|
||||
payService.addConfig(testMchId, newAppId, newConfig);
|
||||
|
||||
|
||||
success = payService.switchover(testMchId, newAppId);
|
||||
System.out.println("切换结果: " + success);
|
||||
System.out.println("当前配置 - MchId: " + payService.getConfig().getMchId() + ", AppId: " + payService.getConfig().getAppId() + ", MchKey: " + payService.getConfig().getMchKey());
|
||||
assert success : "切换到新添加的配置应该成功";
|
||||
assert newAppId.equals(payService.getConfig().getAppId()) : "AppId应该是 " + newAppId;
|
||||
verify(success, "切换到新添加的配置应该成功");
|
||||
verify(newAppId.equals(payService.getConfig().getAppId()), "AppId应该是 " + newAppId);
|
||||
System.out.println("✓ 测试7通过\n");
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user