Last active
July 27, 2019 10:07
-
-
Save Audhil/086754650b573bf0e2ce5e20a7e2b2be 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
@RunWith(AndroidJUnit4::class) | |
class BasicActivityTest { | |
private lateinit var mockServer: MockWebServer | |
@Rule | |
@JvmField | |
val rule = ActivityTestRule(DummyActivity::class.java, false, false) | |
@Before | |
fun setUp() { | |
// init mock server | |
mockServer = MockWebServer() | |
// at port 8080 | |
mockServer.start(8080) | |
} | |
@After | |
fun tearDown() { | |
// shut down | |
mockServer.shutdown() | |
} | |
@Test | |
fun happyTest() { | |
mockServer.dispatcher = MockServerDispatcher.ResponseDispatcher() | |
val intent = Intent(InstrumentationRegistry.getInstrumentation().targetContext, DummyActivity::class.java) | |
// passing valid MOCK url to the activity | |
intent.putExtra( | |
ConstantsUtil.MOCK_URL, | |
mockServer.url("/todos/1").toString() | |
) | |
rule.launchActivity(intent) | |
// checks text in text view | |
Espresso.onView(ViewMatchers.withId(R.id.dddd)) | |
.check(ViewAssertions.matches(ViewMatchers.withText("success!!!"))) | |
} | |
@Test | |
fun unHappyTest() { | |
mockServer.dispatcher = MockServerDispatcher.ErrorDispatcher() | |
val intent = Intent(InstrumentationRegistry.getInstrumentation().targetContext, DummyActivity::class.java) | |
// passing invalid MOCK url to the activity | |
intent.putExtra( | |
ConstantsUtil.MOCK_URL, | |
mockServer.url("jack and jill URL").toString() | |
) | |
rule.launchActivity(intent) | |
// checks text in text view | |
Espresso.onView(ViewMatchers.withId(R.id.dddd)) | |
.check(ViewAssertions.matches(ViewMatchers.withText("somethingWentWrong!!!"))) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment