Skip to content

Commit 7deb25b

Browse files
committed
单元测试代码优化
1 parent ed80cec commit 7deb25b

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpBaseAPITest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public class WxMpBaseAPITest {
2020
protected WxMpServiceImpl wxService;
2121

2222
public void testRefreshAccessToken() throws WxErrorException {
23-
WxMpConfigStorage configStorage = wxService.wxMpConfigStorage;
23+
WxMpConfigStorage configStorage = this.wxService.wxMpConfigStorage;
2424
String before = configStorage.getAccessToken();
25-
wxService.getAccessToken(false);
25+
this.wxService.getAccessToken(false);
2626

2727
String after = configStorage.getAccessToken();
2828
Assert.assertNotEquals(before, after);

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpMediaAPITest.java

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,21 +26,23 @@ public class WxMpMediaAPITest {
2626
@Inject
2727
protected WxMpServiceImpl wxService;
2828

29-
private List<String> media_ids = new ArrayList<String>();
29+
private List<String> media_ids = new ArrayList<>();
3030

3131
@Test(dataProvider="uploadMedia")
3232
public void testUploadMedia(String mediaType, String fileType, String fileName) throws WxErrorException, IOException {
33-
InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName);
34-
WxMediaUploadResult res = wxService.mediaUpload(mediaType, fileType, inputStream);
35-
Assert.assertNotNull(res.getType());
36-
Assert.assertNotNull(res.getCreatedAt());
37-
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
38-
39-
if (res.getMediaId() != null) {
40-
media_ids.add(res.getMediaId());
41-
}
42-
if (res.getThumbMediaId() != null) {
43-
media_ids.add(res.getThumbMediaId());
33+
try(InputStream inputStream = ClassLoader.getSystemResourceAsStream(fileName)){
34+
WxMediaUploadResult res = this.wxService.mediaUpload(mediaType, fileType, inputStream);
35+
Assert.assertNotNull(res.getType());
36+
Assert.assertNotNull(res.getCreatedAt());
37+
Assert.assertTrue(res.getMediaId() != null || res.getThumbMediaId() != null);
38+
39+
if (res.getMediaId() != null) {
40+
this.media_ids.add(res.getMediaId());
41+
}
42+
43+
if (res.getThumbMediaId() != null) {
44+
this.media_ids.add(res.getThumbMediaId());
45+
}
4446
}
4547
}
4648

@@ -56,7 +58,7 @@ public Object[][] uploadMedia() {
5658

5759
@Test(dependsOnMethods = { "testUploadMedia" }, dataProvider="downloadMedia")
5860
public void testDownloadMedia(String media_id) throws WxErrorException {
59-
wxService.mediaDownload(media_id);
61+
this.wxService.mediaDownload(media_id);
6062
}
6163

6264
@DataProvider

weixin-java-mp/src/test/java/me/chanjar/weixin/mp/api/WxMpQrCodeAPITest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,29 @@ public class WxMpQrCodeAPITest {
2222
protected WxMpServiceImpl wxService;
2323

2424
public void testQrCodeCreateTmpTicket() throws WxErrorException {
25-
WxMpQrCodeTicket ticket = wxService.qrCodeCreateTmpTicket(1, null);
25+
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateTmpTicket(1, null);
2626
Assert.assertNotNull(ticket.getUrl());
2727
Assert.assertNotNull(ticket.getTicket());
2828
Assert.assertTrue(ticket.getExpire_seconds() != -1);
2929
}
3030

3131
public void testQrCodeCreateLastTicket() throws WxErrorException {
32-
WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
32+
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
3333
Assert.assertNotNull(ticket.getUrl());
3434
Assert.assertNotNull(ticket.getTicket());
3535
Assert.assertTrue(ticket.getExpire_seconds() == -1);
3636
}
3737

3838
public void testQrCodePicture() throws WxErrorException {
39-
WxMpQrCodeTicket ticket = wxService.qrCodeCreateLastTicket(1);
40-
File file = wxService.qrCodePicture(ticket);
39+
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
40+
File file = this.wxService.qrCodePicture(ticket);
4141
Assert.assertNotNull(file);
4242
}
43+
44+
public void testQrCodePictureUrl() throws WxErrorException {
45+
WxMpQrCodeTicket ticket = this.wxService.qrCodeCreateLastTicket(1);
46+
String url = this.wxService.qrCodePictureUrl(ticket.getTicket());
47+
Assert.assertNotNull(url);
48+
}
4349

4450
}

0 commit comments

Comments
 (0)