Skip to content

Commit 568fa99

Browse files
Copilotbinarywang
andauthored
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>
1 parent 08a6540 commit 568fa99

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

weixin-java-miniapp/src/test/java/cn/binarywang/wx/miniapp/api/impl/WxMaSubscribeServiceImplTest.java

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.binarywang.wx.miniapp.api.impl;
22

33
import cn.binarywang.wx.miniapp.api.WxMaService;
4+
import cn.binarywang.wx.miniapp.api.WxMaSubscribeService;
45
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyRequest;
56
import cn.binarywang.wx.miniapp.bean.WxMaGetUserNotifyResult;
67
import cn.binarywang.wx.miniapp.bean.WxMaServiceNotifyExtRequest;
@@ -14,12 +15,16 @@
1415
import com.google.common.collect.Lists;
1516
import com.google.inject.Inject;
1617
import me.chanjar.weixin.common.error.WxErrorException;
18+
import org.testng.Assert;
1719
import org.testng.annotations.Guice;
1820
import org.testng.annotations.Test;
1921

2022
import java.util.List;
2123

2224
import static org.assertj.core.api.Assertions.assertThat;
25+
import static org.mockito.ArgumentMatchers.anyString;
26+
import static org.mockito.Mockito.mock;
27+
import static org.mockito.Mockito.when;
2328

2429
/**
2530
* 测试类.
@@ -78,38 +83,57 @@ public void testSendSubscribeMsg() throws WxErrorException {
7883

7984
@Test
8085
public void testSetUserNotify() throws WxErrorException {
81-
// TODO 待完善补充,需要真实的 openid、notify_type、notify_code、content_json 参数
86+
WxMaService service = mock(WxMaService.class);
87+
when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");
88+
89+
WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service);
8290
WxMaServiceNotifyRequest request = WxMaServiceNotifyRequest.builder()
8391
.openid("test_openid")
8492
.notifyType(1)
8593
.notifyCode("test_notify_code")
8694
.contentJson("{}")
8795
.build();
88-
this.wxService.getSubscribeService().setUserNotify(request);
96+
subscribeService.setUserNotify(request);
8997
}
9098

9199
@Test
92100
public void testSetUserNotifyExt() throws WxErrorException {
93-
// TODO 待完善补充,需要真实的 openid、notify_type、notify_code、ext_json 参数
101+
WxMaService service = mock(WxMaService.class);
102+
when(service.post(anyString(), anyString())).thenReturn("{\"errcode\":0,\"errmsg\":\"ok\"}");
103+
104+
WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service);
94105
WxMaServiceNotifyExtRequest request = WxMaServiceNotifyExtRequest.builder()
95106
.openid("test_openid")
96107
.notifyType(1)
97108
.notifyCode("test_notify_code")
98109
.extJson("{}")
99110
.build();
100-
this.wxService.getSubscribeService().setUserNotifyExt(request);
111+
subscribeService.setUserNotifyExt(request);
101112
}
102113

103114
@Test
104115
public void testGetUserNotify() throws WxErrorException {
105-
// TODO 待完善补充,需要真实的 openid、notify_type、notify_code 参数
116+
WxMaService service = mock(WxMaService.class);
117+
when(service.post(anyString(), anyString())).thenReturn(
118+
"{\"errcode\":0,\"errmsg\":\"ok\","
119+
+ "\"notify_info\":{"
120+
+ "\"notify_type\":1,"
121+
+ "\"content_json\":\"{\\\"status\\\":1}\","
122+
+ "\"code_state\":0,"
123+
+ "\"code_expire_time\":1700000000"
124+
+ "}}");
125+
126+
WxMaSubscribeService subscribeService = new WxMaSubscribeServiceImpl(service);
106127
WxMaGetUserNotifyRequest request = WxMaGetUserNotifyRequest.builder()
107128
.openid("test_openid")
108129
.notifyCode("test_notify_code")
109130
.notifyType(1)
110131
.build();
111-
WxMaGetUserNotifyResult result = this.wxService.getSubscribeService().getUserNotify(request);
112-
assertThat(result).isNotNull();
113-
System.out.println(result);
132+
WxMaGetUserNotifyResult result = subscribeService.getUserNotify(request);
133+
Assert.assertNotNull(result);
134+
Assert.assertNotNull(result.getNotifyInfo());
135+
Assert.assertEquals(result.getNotifyInfo().getNotifyType().intValue(), 1);
136+
Assert.assertEquals(result.getNotifyInfo().getCodeState().intValue(), 0);
137+
Assert.assertEquals(result.getNotifyInfo().getCodeExpireTime().longValue(), 1700000000L);
114138
}
115139
}

0 commit comments

Comments
 (0)