From 568fa99572243dc5e591c0b1157c8c8dd7314659 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Mon, 20 Apr 2026 03:06:50 +0000 Subject: [PATCH] =?UTF-8?q?test(miniapp):=20=E4=BD=BF=E7=94=A8=20Mockito?= =?UTF-8?q?=20=E9=87=8D=E5=86=99=E6=9C=8D=E5=8A=A1=E5=8D=A1=E7=89=87?= =?UTF-8?q?=E6=B6=88=E6=81=AF=E6=B5=8B=E8=AF=95=E7=94=A8=E4=BE=8B=EF=BC=8C?= =?UTF-8?q?=E9=81=BF=E5=85=8D=E4=BE=9D=E8=B5=96=E7=9C=9F=E5=AE=9E=E5=BE=AE?= =?UTF-8?q?=E4=BF=A1=E5=87=AD=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/ba975cc8-22c0-425a-907e-010d9db12b46 Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com> --- .../impl/WxMaSubscribeServiceImplTest.java | 40 +++++++++++++++---- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java index 602e339c2..c910d121d 100644 --- a/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java +++ b/weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java @@ -1,6 +1,7 @@ package cn.binarywang.wx.miniapp.api.impl; import cn.binarywang.wx.miniapp.api.WxMaService; +import cn.binarywang.wx.miniapp.api.WxMaSubscribeService; import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyRequest; import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyResult; import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyExtRequest; @@ -14,12 +15,16 @@ import cn.binarywang.wx.miniapp.test.ApiTestModule; import com.google.common.collect.Lists; import com.google.inject.Inject; import me.chanjar.weixin.common.error.WxErrorException; +import org.testng.Assert; import org.testng.annotations.Guice; import org.testng.annotations.Test; import java.util.List; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * 测试类. @@ -78,38 +83,57 @@ public class WxMaSubscribeServiceImplTest { @Test public void testSetUserNotify() throws WxErrorException { - // TODO 待完善补充,需要真实的 openid、notify_type、notify_code、content_json 参数 + WxMaService service = mock(WxMaService.class); + when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); + + WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service); WxMaServiceNotifyRequest request = WxMaServiceNotifyRequest.builder() .openid("test_openid") .notifyType(1) .notifyCode("test_notify_code") .contentJson("{}") .build(); - this.wxService.getSubscribeService().setUserNotify(request); + subscribeService.setUserNotify(request); } @Test public void testSetUserNotifyExt() throws WxErrorException { - // TODO 待完善补充,需要真实的 openid、notify_type、notify_code、ext_json 参数 + WxMaService service = mock(WxMaService.class); + when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}"); + + WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service); WxMaServiceNotifyExtRequest request = WxMaServiceNotifyExtRequest.builder() .openid("test_openid") .notifyType(1) .notifyCode("test_notify_code") .extJson("{}") .build(); - this.wxService.getSubscribeService().setUserNotifyExt(request); + subscribeService.setUserNotifyExt(request); } @Test public void testGetUserNotify() throws WxErrorException { - // TODO 待完善补充,需要真实的 openid、notify_type、notify_code 参数 + WxMaService service = mock(WxMaService.class); + when(service.post(anyString(), anyString())).thenReturn( + "{\"errcode\":0,\"errmsg\":\"ok\"," + + "\"notify_info\":{" + + "\"notify_type\":1," + + "\"content_json\":\"{\\\"status\\\":1}\"," + + "\"code_state\":0," + + "\"code_expire_time\":1700000000" + + "}}"); + + WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service); WxMaGetUserNotifyRequest request = WxMaGetUserNotifyRequest.builder() .openid("test_openid") .notifyCode("test_notify_code") .notifyType(1) .build(); - WxMaGetUserNotifyResult result = this.wxService.getSubscribeService().getUserNotify(request); - assertThat(result).isNotNull(); - System.out.println(result); + WxMaGetUserNotifyResult result = subscribeService.getUserNotify(request); + Assert.assertNotNull(result); + Assert.assertNotNull(result.getNotifyInfo()); + Assert.assertEquals(result.getNotifyInfo().getNotifyType().intValue(), 1); + Assert.assertEquals(result.getNotifyInfo().getCodeState().intValue(), 0); + Assert.assertEquals(result.getNotifyInfo().getCodeExpireTime().longValue(), 1700000000L); } }