Skip to content

Commit b44f9b3

Browse files
committed
优化部分测试代码
1 parent fb8de94 commit b44f9b3

File tree

2 files changed

+46
-56
lines changed

2 files changed

+46
-56
lines changed

weixin-java-mp/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@
6565
<artifactId>logback-classic</artifactId>
6666
<scope>test</scope>
6767
</dependency>
68+
<dependency>
69+
<groupId>org.assertj</groupId>
70+
<artifactId>assertj-guava</artifactId>
71+
<scope>test</scope>
72+
</dependency>
6873
<dependency>
6974
<groupId>org.projectlombok</groupId>
7075
<artifactId>lombok</artifactId>

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/impl/WxMpKefuServiceImplTest.java

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@
1010
import me.chanjar.weixin.mp.bean.kefu.request.WxMpKfAccountRequest;
1111
import me.chanjar.weixin.mp.bean.kefu.result.*;
1212
import org.joda.time.DateTime;
13-
import org.testng.*;
1413
import org.testng.annotations.*;
1514

1615
import java.io.File;
1716
import java.util.Date;
1817

18+
import static org.assertj.core.api.Assertions.assertThat;
19+
1920
/**
2021
* 测试客服相关接口
2122
*
@@ -29,150 +30,134 @@ public class WxMpKefuServiceImplTest {
2930
protected WxMpService wxService;
3031

3132
public void testSendKefuMpNewsMessage() throws WxErrorException {
32-
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
33-
.getWxMpConfigStorage();
33+
TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
3434
WxMpKefuMessage message = new WxMpKefuMessage();
3535
message.setMsgType(WxConsts.KefuMsgType.MPNEWS);
3636
message.setToUser(configStorage.getOpenid());
3737
message.setMpNewsMediaId("52R6dL2FxDpM9N1rCY3sYBqHwq-L7K_lz1sPI71idMg");
3838

39-
this.wxService.getKefuService().sendKefuMessage(message);
39+
boolean result = this.wxService.getKefuService().sendKefuMessage(message);
40+
assertThat(result).isTrue();
4041
}
4142

4243
public void testSendKefuMessage() throws WxErrorException {
43-
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
44-
.getWxMpConfigStorage();
44+
TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
4545
WxMpKefuMessage message = new WxMpKefuMessage();
4646
message.setMsgType(WxConsts.KefuMsgType.TEXT);
4747
message.setToUser(configStorage.getOpenid());
48-
message.setContent(
49-
"欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
48+
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
5049

51-
this.wxService.getKefuService().sendKefuMessage(message);
50+
boolean result = this.wxService.getKefuService().sendKefuMessage(message);
51+
assertThat(result).isTrue();
5252
}
5353

5454
public void testSendKefuMessageWithKfAccount() throws WxErrorException {
55-
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
56-
.getWxMpConfigStorage();
55+
TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
5756
WxMpKefuMessage message = new WxMpKefuMessage();
5857
message.setMsgType(WxConsts.KefuMsgType.TEXT);
5958
message.setToUser(configStorage.getOpenid());
6059
message.setKfAccount(configStorage.getKfAccount());
61-
message.setContent(
62-
"欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
60+
message.setContent("欢迎欢迎,热烈欢迎\n换行测试\n超链接:<a href=\"http://www.baidu.com\">Hello World</a>");
6361

64-
this.wxService.getKefuService().sendKefuMessage(message);
62+
boolean result = this.wxService.getKefuService().sendKefuMessage(message);
63+
assertThat(result).isTrue();
6564
}
6665

6766
public void testKfList() throws WxErrorException {
6867
WxMpKfList kfList = this.wxService.getKefuService().kfList();
69-
Assert.assertNotNull(kfList);
68+
assertThat(kfList).isNotNull();
7069
for (WxMpKfInfo k : kfList.getKfList()) {
7170
System.err.println(k);
7271
}
7372
}
7473

7574
public void testKfOnlineList() throws WxErrorException {
76-
WxMpKfOnlineList kfOnlineList = this.wxService.getKefuService()
77-
.kfOnlineList();
78-
Assert.assertNotNull(kfOnlineList);
75+
WxMpKfOnlineList kfOnlineList = this.wxService.getKefuService().kfOnlineList();
76+
assertThat(kfOnlineList).isNotNull();
7977
for (WxMpKfInfo k : kfOnlineList.getKfOnlineList()) {
8078
System.err.println(k);
8179
}
8280
}
8381

8482
@DataProvider
8583
public Object[][] getKfAccount() {
86-
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
87-
.getWxMpConfigStorage();
84+
TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
8885
return new Object[][]{{configStorage.getKfAccount()}};
8986
}
9087

9188
@Test(dataProvider = "getKfAccount")
9289
public void testKfAccountAdd(String kfAccount) throws WxErrorException {
9390
WxMpKfAccountRequest request = WxMpKfAccountRequest.builder()
9491
.kfAccount(kfAccount).nickName("我晕").build();
95-
Assert.assertTrue(this.wxService.getKefuService().kfAccountAdd(request));
92+
assertThat(this.wxService.getKefuService().kfAccountAdd(request)).isTrue();
9693
}
9794

9895
@Test(dependsOnMethods = {
9996
"testKfAccountAdd"}, dataProvider = "getKfAccount")
10097
public void testKfAccountUpdate(String kfAccount) throws WxErrorException {
10198
WxMpKfAccountRequest request = WxMpKfAccountRequest.builder()
10299
.kfAccount(kfAccount).nickName("我晕").build();
103-
Assert.assertTrue(this.wxService.getKefuService().kfAccountUpdate(request));
100+
assertThat(this.wxService.getKefuService().kfAccountUpdate(request)).isTrue();
104101
}
105102

106103
@Test(dependsOnMethods = {
107104
"testKfAccountAdd"}, dataProvider = "getKfAccount")
108105
public void testKfAccountInviteWorker(String kfAccount) throws WxErrorException {
109106
WxMpKfAccountRequest request = WxMpKfAccountRequest.builder()
110107
.kfAccount(kfAccount).inviteWx(" ").build();
111-
Assert.assertTrue(this.wxService.getKefuService().kfAccountInviteWorker(request));
108+
assertThat(this.wxService.getKefuService().kfAccountInviteWorker(request)).isTrue();
112109
}
113110

114-
@Test(dependsOnMethods = {
115-
"testKfAccountUpdate"}, dataProvider = "getKfAccount")
116-
public void testKfAccountUploadHeadImg(String kfAccount)
117-
throws WxErrorException {
111+
@Test(dependsOnMethods = {"testKfAccountUpdate", "testKfAccountAdd"}, dataProvider = "getKfAccount")
112+
public void testKfAccountUploadHeadImg(String kfAccount) throws WxErrorException {
118113
File imgFile = new File("src\\test\\resources\\mm.jpeg");
119-
boolean result = this.wxService.getKefuService()
120-
.kfAccountUploadHeadImg(kfAccount, imgFile);
121-
Assert.assertTrue(result);
114+
boolean result = this.wxService.getKefuService().kfAccountUploadHeadImg(kfAccount, imgFile);
115+
assertThat(result).isTrue();
122116
}
123117

124118
@Test(dataProvider = "getKfAccount")
125119
public void testKfAccountDel(String kfAccount) throws WxErrorException {
126120
boolean result = this.wxService.getKefuService().kfAccountDel(kfAccount);
127-
Assert.assertTrue(result);
121+
assertThat(result).isTrue();
128122
}
129123

130124
@DataProvider
131125
public Object[][] getKfAccountAndOpenid() {
132-
TestConfigStorage configStorage = (TestConfigStorage) this.wxService
133-
.getWxMpConfigStorage();
134-
return new Object[][]{
135-
{configStorage.getKfAccount(), configStorage.getOpenid()}};
126+
TestConfigStorage configStorage = (TestConfigStorage) this.wxService.getWxMpConfigStorage();
127+
return new Object[][]{{configStorage.getKfAccount(), configStorage.getOpenid()}};
136128
}
137129

138130
@Test(dataProvider = "getKfAccountAndOpenid")
139-
public void testKfSessionCreate(String kfAccount, String openid)
140-
throws WxErrorException {
141-
boolean result = this.wxService.getKefuService().kfSessionCreate(openid,
142-
kfAccount);
143-
Assert.assertTrue(result);
131+
public void testKfSessionCreate(String kfAccount, String openid) throws WxErrorException {
132+
boolean result = this.wxService.getKefuService().kfSessionCreate(openid, kfAccount);
133+
assertThat(result).isTrue();
144134
}
145135

146136
@Test(dataProvider = "getKfAccountAndOpenid")
147137
public void testKfSessionClose(String kfAccount, String openid)
148138
throws WxErrorException {
149-
boolean result = this.wxService.getKefuService().kfSessionClose(openid,
150-
kfAccount);
151-
Assert.assertTrue(result);
139+
boolean result = this.wxService.getKefuService().kfSessionClose(openid, kfAccount);
140+
assertThat(result).isTrue();
152141
}
153142

154143
@Test(dataProvider = "getKfAccountAndOpenid")
155-
public void testKfSessionGet(@SuppressWarnings("unused") String kfAccount,
156-
String openid) throws WxErrorException {
157-
WxMpKfSessionGetResult result = this.wxService.getKefuService()
158-
.kfSessionGet(openid);
159-
Assert.assertNotNull(result);
144+
public void testKfSessionGet(@SuppressWarnings("unused") String kfAccount, String openid) throws WxErrorException {
145+
WxMpKfSessionGetResult result = this.wxService.getKefuService().kfSessionGet(openid);
146+
assertThat(result).isNotNull();
160147
System.err.println(result);
161148
}
162149

163150
@Test(dataProvider = "getKfAccount")
164151
public void testKfSessionList(String kfAccount) throws WxErrorException {
165-
WxMpKfSessionList result = this.wxService.getKefuService()
166-
.kfSessionList(kfAccount);
167-
Assert.assertNotNull(result);
152+
WxMpKfSessionList result = this.wxService.getKefuService().kfSessionList(kfAccount);
153+
assertThat(result).isNotNull();
168154
System.err.println(result);
169155
}
170156

171157
@Test
172158
public void testKfSessionGetWaitCase() throws WxErrorException {
173-
WxMpKfSessionWaitCaseList result = this.wxService.getKefuService()
174-
.kfSessionGetWaitCase();
175-
Assert.assertNotNull(result);
159+
WxMpKfSessionWaitCaseList result = this.wxService.getKefuService().kfSessionGetWaitCase();
160+
assertThat(result).isNotNull();
176161
System.err.println(result);
177162
}
178163

@@ -181,7 +166,7 @@ public void testKfMsgList() throws WxErrorException {
181166
Date startTime = DateTime.now().minusDays(1).toDate();
182167
Date endTime = DateTime.now().minusDays(0).toDate();
183168
WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime, endTime, 1L, 50);
184-
Assert.assertNotNull(result);
169+
assertThat(result).isNotNull();
185170
System.err.println(result);
186171
}
187172

@@ -190,7 +175,7 @@ public void testKfMsgListAll() throws WxErrorException {
190175
Date startTime = DateTime.now().minusDays(1).toDate();
191176
Date endTime = DateTime.now().minusDays(0).toDate();
192177
WxMpKfMsgList result = this.wxService.getKefuService().kfMsgList(startTime, endTime);
193-
Assert.assertNotNull(result);
178+
assertThat(result).isNotNull();
194179
System.err.println(result);
195180
}
196181
}

0 commit comments

Comments
 (0)