Skip to content

Instantly share code, notes, and snippets.

@johngorithm
Last active March 13, 2019 18:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johngorithm/4263ed160bb83136003a277024db3e7d to your computer and use it in GitHub Desktop.
Save johngorithm/4263ed160bb83136003a277024db3e7d to your computer and use it in GitHub Desktop.
@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