Skip to content

Instantly share code, notes, and snippets.

@Audhil
Last active July 27, 2019 10:07
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 Audhil/086754650b573bf0e2ce5e20a7e2b2be to your computer and use it in GitHub Desktop.
Save Audhil/086754650b573bf0e2ce5e20a7e2b2be to your computer and use it in GitHub Desktop.
@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