1
0
mirror of synced 2026-04-21 17:09:31 +08:00

test(miniapp): 使用 Mockito 重写服务卡片消息测试用例,避免依赖真实微信凭据

Agent-Logs-Url: https://github.com/binarywang/WxJava/sessions/ba975cc8-22c0-425a-907e-010d9db12b46

Co-authored-by: binarywang <1343140+binarywang@users.noreply.github.com>
This commit is contained in:
copilot-swe-agent[bot]
2026-04-20 03:06:50 +00:00
committed by GitHub
parent 08a6540362
commit 568fa99572

View File

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