Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
@Test
public void testTeachersAreDisplayed() throws IOException {
/**
* Setting up mockWebServer at localhost:9900.
*/
MockWebServer server = new MockWebServer();
// start the server at port 9900
server.start(9900);
/**
* Get the target application context during testing and cast onto the TestDemoApplication
*/
TestDemoApplication testApp = (TestDemoApplication) InstrumentationRegistry.getTargetContext().getApplicationContext();
// Set the base url of the test app using the url of the mocked local server
testApp.setBaseUrl(server.url("/").toString());
// Enqueue the response you want the server to respond with when querried in the course to this test.
server.enqueue(new MockResponse().setBody(SUCCESS_RESPONSE));
// LAUNCH ACTIVITY
mainActivityTestRule.launchActivity(null);
// UI testing
onView(withText("John Doe")).check(matches(isDisplayed()));
onView(withText("William Smith")).check(matches(isDisplayed()));
// Shut down the server when done with testing
server.shutdown();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.