I want to return stub response when Android app send http request that contain /api/Profile/Favorite?value=%7B%22ID%22%3A11821%7D
So here my test (using WireMock framework)
@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
}
}
But when I start test method testSendRequestEspresso I get production response from production server.
Here request from Android device:
http://mycompany.com/api/Profile/Favorite?value=%7B%22ID%22%3A11821%7D
Here response:
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"}
As you can see I'm NOT get stub response: {This_is_mock_response} from WireMock. Why?