Skip to content

Commit 30608d2

Browse files
Copilotbinarywang
andcommitted
改进代码一致性:使用GsonParser进行错误检查,使用JsonObject构建测试数据
Co-authored-by: binarywang <[email protected]>
1 parent a88fc71 commit 30608d2

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +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-
WxCpTpAdmin result = WxCpTpAdmin.fromJson(responseContent);
80-
if (result.getErrcode() != null && result.getErrcode() != 0) {
79+
JsonObject respObj = GsonParser.parse(responseContent);
80+
if (respObj.get(WxConsts.ERR_CODE).getAsInt() != 0) {
8181
throw new WxErrorException(WxError.fromJson(responseContent, WxType.CP));
8282
}
83-
return result;
83+
return WxCpTpAdmin.fromJson(responseContent);
8484
}
8585

8686
}

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,27 @@ public void testGet() throws Exception {
140140
*/
141141
@Test
142142
public void testGetAdminList() throws Exception {
143-
String returnJson = "{\"errcode\": 0,\"errmsg\": \"ok\",\"admin\": [{\"userid\": \"zhangsan\"," +
144-
"\"open_userid\": \"woAJ2GCAAAXtWyujaWJHDDGi0mACH71w\",\"auth_type\": 1}," +
145-
"{\"userid\": \"lisi\",\"open_userid\": \"woAJ2GCAAAXtWyujaWJHDDGi0mACH72w\",\"auth_type\": 2}]}";
143+
// 构建响应JSON
144+
JsonObject admin1 = new JsonObject();
145+
admin1.addProperty("userid", "zhangsan");
146+
admin1.addProperty("open_userid", "woAJ2GCAAAXtWyujaWJHDDGi0mACH71w");
147+
admin1.addProperty("auth_type", 1);
148+
149+
JsonObject admin2 = new JsonObject();
150+
admin2.addProperty("userid", "lisi");
151+
admin2.addProperty("open_userid", "woAJ2GCAAAXtWyujaWJHDDGi0mACH72w");
152+
admin2.addProperty("auth_type", 2);
153+
154+
com.google.gson.JsonArray adminArray = new com.google.gson.JsonArray();
155+
adminArray.add(admin1);
156+
adminArray.add(admin2);
157+
158+
JsonObject returnJsonObj = new JsonObject();
159+
returnJsonObj.addProperty("errcode", 0);
160+
returnJsonObj.addProperty("errmsg", "ok");
161+
returnJsonObj.add("admin", adminArray);
162+
String returnJson = returnJsonObj.toString();
163+
146164
JsonObject requestJson = new JsonObject();
147165
requestJson.addProperty("agentid", 9);
148166
final WxCpConfigStorage configStorage = new WxCpDefaultConfigImpl();

0 commit comments

Comments
 (0)