Last active
March 13, 2019 18:29
-
-
Save johngorithm/e09870724220be6e398c4367a6bb4d19 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.jxw.graphql; | |
import android.support.test.InstrumentationRegistry; | |
import android.support.test.espresso.IdlingRegistry; | |
import android.support.test.espresso.idling.CountingIdlingResource; | |
import android.support.test.runner.AndroidJUnit4; | |
import android.support.test.rule.ActivityTestRule; | |
import com.jxw.graphql.test_utils.TestDemoApplication; | |
import com.jxw.graphql.utils.EspressoIdlingResource; | |
import com.jxw.graphql.view.MainActivity; | |
import static android.support.test.espresso.Espresso.onView; | |
import static android.support.test.espresso.assertion.ViewAssertions.matches; | |
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed; | |
import static android.support.test.espresso.matcher.ViewMatchers.withId; | |
import static android.support.test.espresso.matcher.ViewMatchers.withText; | |
import static org.hamcrest.CoreMatchers.not; | |
import org.junit.After; | |
import org.junit.Before; | |
import org.junit.Rule; | |
import org.junit.Test; | |
import org.junit.runner.RunWith; | |
import java.io.IOException; | |
import okhttp3.mockwebserver.MockResponse; | |
import okhttp3.mockwebserver.MockWebServer; | |
@RunWith(AndroidJUnit4.class) | |
public class TutorialVersionMainActivityTest { | |
private CountingIdlingResource countingIdlingResource = EspressoIdlingResource.getCountIdlingResource(); | |
/** | |
* TODO: Move the data containing variables into a data class | |
*/ | |
private static final String SUCCESS_RESPONSE = "{" + | |
"\"data\":{" + | |
" \"allTeachers\":[" + | |
" {" + | |
" \"name\":\"John Doe\"," + | |
" \"subject\":\"cs300\"," + | |
" \"__typename\":\"Teacher\"," + | |
" \"id\":\"cjs1ucrri6yqe016794qlaclq\"," + | |
" \"createdAt\":\"2019-02-12T14:07:53.000Z\"" + | |
" }," + | |
" {" + | |
" \"name\":\"William Smith\"," + | |
" \"subject\":\"SLT101\"," + | |
" \"__typename\":\"Teacher\"," + | |
" \"id\":\"cjs1uixtu1f6g019507d0ksrk\"," + | |
" \"createdAt\":\"2019-02-12T14:12:41.000Z\"" + | |
" }" + | |
" ]" + | |
"}}"; | |
@Rule | |
public ActivityTestRule<MainActivity> mainActivityTestRule = new ActivityTestRule<>(MainActivity.class, true, false); | |
@Before | |
public void setUp() { | |
// Espresso Idling Resource registration | |
IdlingRegistry.getInstance().register(countingIdlingResource); | |
} | |
@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(); | |
} | |
@After | |
public void tearDown() { | |
IdlingRegistry.getInstance().unregister(countingIdlingResource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment