|
| 1 | +package cn.binarywang.wx.miniapp.api.impl; |
| 2 | + |
| 3 | +import cn.binarywang.wx.miniapp.api.WxMaService; |
| 4 | +import com.google.gson.JsonObject; |
| 5 | +import me.chanjar.weixin.common.error.WxErrorException; |
| 6 | +import me.chanjar.weixin.common.util.json.GsonParser; |
| 7 | +import org.mockito.ArgumentCaptor; |
| 8 | +import org.testng.annotations.Test; |
| 9 | + |
| 10 | +import static cn.binarywang.wx.miniapp.constant.WxMaApiUrlConstants.User.GET_PHONE_NUMBER_URL; |
| 11 | +import static org.mockito.ArgumentMatchers.anyString; |
| 12 | +import static org.mockito.ArgumentMatchers.eq; |
| 13 | +import static org.mockito.Mockito.mock; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | +import static org.testng.Assert.assertEquals; |
| 17 | +import static org.testng.Assert.assertFalse; |
| 18 | + |
| 19 | +/** |
| 20 | + * {@link WxMaUserServiceImpl} 获取手机号接口的单元测试。 |
| 21 | + */ |
| 22 | +public class WxMaUserServiceImplPhoneNumberTest { |
| 23 | + |
| 24 | + @Test |
| 25 | + public void shouldSendOpenidWhenGettingPhoneNumber() throws WxErrorException { |
| 26 | + WxMaService wxMaService = mock(WxMaService.class); |
| 27 | + when(wxMaService.post(anyString(), anyString())).thenReturn("{\"phone_info\":{}}"); |
| 28 | + |
| 29 | + new WxMaUserServiceImpl(wxMaService).getPhoneNumber("phone-code", "user-openid"); |
| 30 | + |
| 31 | + ArgumentCaptor<String> requestBody = ArgumentCaptor.forClass(String.class); |
| 32 | + verify(wxMaService).post(eq(GET_PHONE_NUMBER_URL), requestBody.capture()); |
| 33 | + JsonObject request = GsonParser.parse(requestBody.getValue()); |
| 34 | + assertEquals(request.get("code").getAsString(), "phone-code"); |
| 35 | + assertEquals(request.get("openid").getAsString(), "user-openid"); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void shouldIgnoreBlankOpenidWhenGettingPhoneNumber() throws WxErrorException { |
| 40 | + WxMaService wxMaService = mock(WxMaService.class); |
| 41 | + when(wxMaService.post(anyString(), anyString())).thenReturn("{\"phone_info\":{}}"); |
| 42 | + |
| 43 | + new WxMaUserServiceImpl(wxMaService).getPhoneNumber("phone-code", " "); |
| 44 | + |
| 45 | + ArgumentCaptor<String> requestBody = ArgumentCaptor.forClass(String.class); |
| 46 | + verify(wxMaService).post(eq(GET_PHONE_NUMBER_URL), requestBody.capture()); |
| 47 | + JsonObject request = GsonParser.parse(requestBody.getValue()); |
| 48 | + assertFalse(request.has("openid")); |
| 49 | + } |
| 50 | +} |
0 commit comments