Skip to content

Commit a88fc71

Browse files
Copilotbinarywang
andcommitted
修复代码审查问题:添加错误检查和改进测试代码
Co-authored-by: binarywang <[email protected]>
1 parent cfde76e commit a88fc71

File tree

2 files changed

+11
-4
lines changed

2 files changed

+11
-4
lines changed

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,11 @@ public WxCpTpAdmin getAdminList(Integer agentId) throws WxErrorException {
7676
jsonObject.addProperty("agentid", agentId);
7777
String url = this.mainService.getWxCpConfigStorage().getApiUrl(AGENT_GET_ADMIN_LIST);
7878
String responseContent = this.mainService.post(url, jsonObject.toString());
79-
return WxCpTpAdmin.fromJson(responseContent);
79+
WxCpTpAdmin result = WxCpTpAdmin.fromJson(responseContent);
80+
if (result.getErrcode() != null && result.getErrcode() != 0) {
81+
throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
82+
}
83+
return result;
8084
}
8185

8286
}

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/api/impl/WxCpAgentServiceImplTest.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package me.chanjar.weixin.cp.api.impl;
22

3+
import com.google.gson.JsonObject;
34
import com.google.inject.Inject;
45
import me.chanjar.weixin.common.error.WxErrorException;
56
import me.chanjar.weixin.cp.api.ApiTestModule;
@@ -93,7 +94,7 @@ public void testGetAdminList() throws WxErrorException {
9394
me.chanjar.weixin.cp.bean.WxCpTpAdmin adminList = this.wxCpService.getAgentService().getAdminList(agentId);
9495

9596
assertThat(adminList).isNotNull();
96-
assertThat(adminList.getErrCode()).isEqualTo(0);
97+
assertThat(adminList.getErrcode()).isEqualTo(0L);
9798
}
9899

99100
/**
@@ -142,15 +143,17 @@ public void testGetAdminList() throws Exception {
142143
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"admin\": [{\"userid\": \"zhangsan\"," +
143144
"\"open_userid\": \"woAJ2GCAAAXtWyujaWJHDDGi0mACH71w\",\"auth_type\": 1}," +
144145
"{\"userid\": \"lisi\",\"open_userid\": \"woAJ2GCAAAXtWyujaWJHDDGi0mACH72w\",\"auth_type\": 2}]}";
146+
JsonObject requestJson = new JsonObject();
147+
requestJson.addProperty("agentid", 9);
145148
final WxCpConfigStorage configStorage = new WxCpDefaultConfigImpl();
146149
when(wxService.getWxCpConfigStorage()).thenReturn(configStorage);
147-
when(wxService.post(configStorage.getApiUrl(WxCpApiPathConsts.Agent.AGENT_GET_ADMIN_LIST), "{\"agentid\":9}")).thenReturn(returnJson);
150+
when(wxService.post(configStorage.getApiUrl(WxCpApiPathConsts.Agent.AGENT_GET_ADMIN_LIST), requestJson.toString())).thenReturn(returnJson);
148151
when(wxService.getAgentService()).thenReturn(new WxCpAgentServiceImpl(wxService));
149152

150153
WxCpAgentService wxAgentService = this.wxService.getAgentService();
151154
me.chanjar.weixin.cp.bean.WxCpTpAdmin adminList = wxAgentService.getAdminList(9);
152155

153-
assertEquals(0, adminList.getErrCode().intValue());
156+
assertEquals(0, adminList.getErrcode().intValue());
154157
assertEquals(2, adminList.getAdmin().size());
155158
assertEquals("zhangsan", adminList.getAdmin().get(0).getUserId());
156159
assertEquals("woAJ2GCAAAXtWyujaWJHDDGi0mACH71w", adminList.getAdmin().get(0).getOpenUserId());

0 commit comments

Comments
 (0)