gpt4 book ai didi

Android,WireMock.stubFor 不返回 stub 响应

转载 作者:太空狗 更新时间:2023-10-29 14:43:01 25 4
gpt4 key购买 nike

我想在 Android 应用发送包含 /api/Profile/Favorite?value=%7B%22ID%22%3A11821%7D 的 http 请求时返回 stub 响应

所以这里是我的测试(使用 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 的 stub 响应:{This_is_mock_response}。为什么?

最佳答案

您请求的 baseurl 应该是http://127.0.0.1:8089。因为那是你设置模拟的地方?我错过了什么吗?

关于Android,WireMock.stubFor 不返回 stub 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44827579/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com