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); } }