I am trying to intercept the network calls made in my espresso test so that I can override the response with the local copy and make the tests more deterministic .The problem I am facing is even though I setup the MockWebServer correctly I never see any of the http calls being intercepted by it when I make network calls from within my espresso test.I have spent almost two days reading about this but could not figure out the problem.
public class FragmenEspressoTest extends InstrumentationTest<SomeActivity> {
private MockWebServer mockWebServer;
@Override
public Class<HomeDrawerActivity> getActivityClass() {
return HomeDrawerActivity.class;
}
@Before
public void setUp() {
super.setUp();
mockWebServer = new MockWebServer();
mockWebServer.setDispatcher(dispatcher);
try {
mockWebServer.start();
} catch (IOException e) {
e.printStackTrace();
}
}
final Dispatcher dispatcher = new Dispatcher() {
@Override
public MockResponse dispatch(RecordedRequest request) throws InterruptedException {
if (request.getPath().equals("/v1/login/auth/")){
return new MockResponse().setResponseCode(200);
} else if (request.getPath().equals("v1/check/version/")){
return new MockResponse().setResponseCode(200).setBody("version=9");
} else if (request.getPath().equals("/v1/profile/info")) {
return new MockResponse().setResponseCode(200).setBody("{\\\"info\\\":{\\\"name\":\"Lucas Albuquerque\",\"age\":\"21\",\"gender\":\"male\"}}");
}
return new MockResponse().setResponseCode(404);
}
};
@After
public void tearDown() {
super.tearDown();
try {
mockWebServer.shutdown();
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void verifyUITest() {
//verify some UI do some network calls
}
}