当Android应用发送包含 / api /个人资料/收藏夹?值=%7B%22ID%22%3A11821%7D
的http请求时,我想返回存根响应所以我的测试(使用WireMock框架)
@RunWith(AndroidJUnit4.class)
public class ContactsFragmentTransportTest extends ActivityInstrumentationTestCase2 {
public ContactsFragmentTransportTest() {
super(ContactsFragment.class);
}
@Rule
public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>(MainActivity.class, true, false);
@Before
@Override
public void setUp() throws Exception {
super.setUp();
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().port(8089));
wireMockServer.start();
WireMock.configureFor("127.0.0.1", wireMockServer.port());
}
@Test
public void testSendRequestEspresso() throws Exception {
stubFor(get(urlMatching("/api/Profile/Favorite?value=%7B%22ID%22%3A11821%7D"))
.willReturn(aResponse()
.withStatus(200)
.withBody("{This_is_mock_response}")));
// some code that send http request
}
}
但是,当我开始测试方法 testSendRequestEspresso 时,我从生产服务器获得生产响应。
此处来自Android设备的请求:
http://mycompany.com/api/Profile/Favorite?value=%7B%22ID%22%3A11821%7D
在这里回复:
StatusCode = 200
HEADERS =
[Cache-Control = no-cache]
[Connection = Keep-Alive]
[Content-Length = 41]
[Content-Type = application/json; charset=utf-8]
[Date = Thu, 29 Jun 2017 14:10:42 GMT]
[Expires = -1]
[Pragma = no-cache]
[Server = Microsoft-IIS/8.5]
[X-Android-Received-Millis = 1498745395359]
[X-Android-Response-Source = NETWORK 200]
[X-Android-Selected-Protocol = http/1.1]
[X-Android-Sent-Millis = 1498745395320]
[X-AspNet-Version = 4.0.30319]
[X-Powered-By = ASP.NET]
BODY = {"Code":"CodeSuccess","Message":"Succes"}
正如您所看到的,我没有从WireMock获得存根响应: {This_is_mock_response} 。 为什么呢?
答案 0 :(得分:0)
您的请求的baseurl应该是
http://127.0.0.1:8089
。因为那是你设置模拟的权利吗?
我错过了什么吗?