Android, WireMock.stubFor 不返回存根响应.



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

所以这是我的测试(使用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时,我从生产服务器得到生产响应。

这里从安卓设备请求:

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}。 为什么?

请求的基本网址应该是 http://127.0.0.1:8089 .因为那是你设置模拟的地方? 我错过了什么吗?

最新更新