From 3f32c4488e9d8cd94c53e0e5843ec68c128b2433 Mon Sep 17 00:00:00 2001 From: YunaiV Date: Tue, 24 Oct 2023 08:12:38 +0800 Subject: [PATCH] =?UTF-8?q?pay=EF=BC=9A=E4=BF=AE=E5=A4=8D=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95=E7=9A=84=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/order/PayOrderServiceTest.java | 31 +++++++++---------- .../service/refund/PayRefundServiceTest.java | 15 ++++----- 2 files changed, 20 insertions(+), 26 deletions(-) diff --git a/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceTest.java b/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceTest.java index c61672b44..cf72aa819 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceTest.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/order/PayOrderServiceTest.java @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.pay.service.order; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.pay.core.client.PayClient; -import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; import cn.iocoder.yudao.framework.pay.core.client.dto.order.PayOrderRespDTO; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; import cn.iocoder.yudao.framework.pay.core.enums.order.PayOrderDisplayModeEnum; @@ -67,8 +66,6 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { @Resource private PayOrderExtensionMapper orderExtensionMapper; - @MockBean - private PayClientFactory payClientFactory; @MockBean private PayProperties properties; @MockBean @@ -351,7 +348,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { .thenReturn(channel); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法() PayOrderRespDTO unifiedOrderResp = randomPojo(PayOrderRespDTO.class, o -> o.setChannelErrorCode("001").setChannelErrorMsg("模拟异常")); @@ -405,7 +402,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { .thenReturn(channel); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(支付渠道的调用) PayOrderRespDTO unifiedOrderResp = randomPojo(PayOrderRespDTO.class, o -> o.setChannelErrorCode(null).setChannelErrorMsg(null) .setDisplayMode(PayOrderDisplayModeEnum.URL.getMode()).setDisplayContent("tudou")); @@ -463,7 +460,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient 已支付) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); + when(channelService.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); when(client.getOrder(eq(orderExtension.getNo()))).thenReturn(randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus()))); @@ -482,7 +479,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient 已支付) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); + when(channelService.getPayClient(eq(orderExtension.getChannelId()))).thenReturn(client); when(client.getOrder(eq(orderExtension.getNo()))).thenReturn(randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.WAITING.getStatus()))); @@ -639,7 +636,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // 准备参数 PayChannelDO channel = randomPojo(PayChannelDO.class, o -> o.setId(10L) - .setFeeRate(0.1D)); + .setFeeRate(10D)); PayOrderRespDTO notify = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusRespEnum.SUCCESS.getStatus()) .setOutTradeNo("P110")); @@ -656,12 +653,12 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { .setChannelId(10L).setChannelCode(channel.getCode()) .setSuccessTime(notify.getSuccessTime()).setExtensionId(orderExtension.getId()).setNo(orderExtension.getNo()) .setChannelOrderNo(notify.getChannelOrderNo()).setChannelUserId(notify.getChannelUserId()) - .setChannelFeeRate(0.1D).setChannelFeePrice(1); + .setChannelFeeRate(10D).setChannelFeePrice(1); assertPojoEquals(order, orderMapper.selectOne(null), "updateTime", "updater"); // 断言,调用 verify(notifyService).createPayNotifyTask(eq(PayNotifyTypeEnum.ORDER.getType()), - eq(orderExtension.getOrderId())); + eq(orderExtension.getOrderId())); } @Test @@ -874,7 +871,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 异常) when(client.getOrder(any())).thenThrow(new RuntimeException()); @@ -901,7 +898,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 成功返回) PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus())); @@ -935,7 +932,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 成功返回) PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.CLOSED.getStatus())); @@ -966,7 +963,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // 调用 int count = orderService.expireOrder(); @@ -1013,7 +1010,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 退款返回) PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.REFUND.getStatus())); @@ -1047,7 +1044,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 成功返回) PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.SUCCESS.getStatus())); @@ -1081,7 +1078,7 @@ public class PayOrderServiceTest extends BaseDbAndRedisUnitTest { orderExtensionMapper.insert(orderExtension); // mock 方法(PayClient) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(PayClient 关闭返回) PayOrderRespDTO respDTO = randomPojo(PayOrderRespDTO.class, o -> o.setStatus(PayOrderStatusEnum.CLOSED.getStatus())); diff --git a/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceTest.java b/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceTest.java index 58a086c6b..a9296f93f 100755 --- a/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceTest.java +++ b/yudao-module-pay/yudao-module-pay-biz/src/test/java/cn/iocoder/yudao/module/pay/service/refund/PayRefundServiceTest.java @@ -3,7 +3,6 @@ package cn.iocoder.yudao.module.pay.service.refund; import cn.hutool.extra.spring.SpringUtil; import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.pay.core.client.PayClient; -import cn.iocoder.yudao.framework.pay.core.client.PayClientFactory; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundRespDTO; import cn.iocoder.yudao.framework.pay.core.client.dto.refund.PayRefundUnifiedReqDTO; import cn.iocoder.yudao.framework.pay.core.enums.channel.PayChannelEnum; @@ -67,8 +66,6 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { @MockBean private PayProperties payProperties; @MockBean - private PayClientFactory payClientFactory; - @MockBean private PayOrderService orderService; @MockBean private PayAppService appService; @@ -335,7 +332,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { when(channelService.validPayChannel(eq(1L))).thenReturn(channel); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 数据(refund 已存在) PayRefundDO refund = randomPojo(PayRefundDO.class, o -> o.setAppId(1L).setMerchantRefundId("200")); @@ -367,7 +364,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { when(channelService.validPayChannel(eq(10L))).thenReturn(channel); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(client 调用发生异常) when(client.unifiedRefund(any(PayRefundUnifiedReqDTO.class))).thenThrow(new RuntimeException()); @@ -411,14 +408,14 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { when(channelService.validPayChannel(eq(10L))).thenReturn(channel); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(client 成功) PayRefundRespDTO refundRespDTO = randomPojo(PayRefundRespDTO.class); when(client.unifiedRefund(argThat(unifiedReqDTO -> { assertNotNull(unifiedReqDTO.getOutRefundNo()); assertThat(unifiedReqDTO) .extracting("payPrice", "refundPrice", "outTradeNo", - "notifyUrl", "reason") + "notifyUrl", "reason") .containsExactly(order.getPrice(), reqDTO.getPrice(), order.getNo(), "http://127.0.0.1/10", reqDTO.getReason()); return true; @@ -668,7 +665,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { refundMapper.insert(refund); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(client 返回指定状态) PayRefundRespDTO respDTO = randomPojo(PayRefundRespDTO.class, o -> o.setStatus(status)); when(client.getRefund(eq("P110"), eq("R220"))).thenReturn(respDTO); @@ -690,7 +687,7 @@ public class PayRefundServiceTest extends BaseDbAndRedisUnitTest { refundMapper.insert(refund); // mock 方法(client) PayClient client = mock(PayClient.class); - when(payClientFactory.getPayClient(eq(10L))).thenReturn(client); + when(channelService.getPayClient(eq(10L))).thenReturn(client); // mock 方法(client 抛出异常) when(client.getRefund(eq("P110"), eq("R220"))).thenThrow(new RuntimeException());