Skip to content

Instantly share code, notes, and snippets.

@Audhil
Created August 23, 2020 17:13
Show Gist options
  • Save Audhil/caeac044766d2f1793a09e750bb6451f to your computer and use it in GitHub Desktop.
Save Audhil/caeac044766d2f1793a09e750bb6451f to your computer and use it in GitHub Desktop.
instrumentation tests - actual test cases
@UninstallModules(APIModule::class, OtherModule::class)
@HiltAndroidTest
class MainActivityTest {
@get:Rule
var hiltRule = HiltAndroidRule(this)
@Rule
@JvmField
val activityRule = ActivityTestRule(MainActivity::class.java, true, false)
private lateinit var mockServer: MockWebServer
@Before
fun setUp() {
mockServer = MockWebServer()
mockServer.start(8080)
}
@After
fun tearDown() =
mockServer.shutdown()
@Test
fun happyTestCase() {
mockServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return MockResponse()
.setResponseCode(200)
.setBody(FileReader.readStringFromFile("success_response.json"))
}
}
activityRule.launchActivity(null)
Espresso.onView(withId(R.id.progress_bar)).run {
check(ViewAssertions.matches(not(ViewMatchers.isDisplayed())))
}
Espresso.onView(withId(R.id.repo_recycler_view)).run {
check(ViewAssertions.matches(ViewMatchers.isDisplayed()))
}
}
@Test
fun unHappyTestCase() {
mockServer.dispatcher = object : Dispatcher() {
override fun dispatch(request: RecordedRequest): MockResponse {
return MockResponse().throttleBody(1024, 5, TimeUnit.SECONDS)
}
}
activityRule.launchActivity(null)
Espresso.onView(withId(R.id.retry_btn)).run {
check(ViewAssertions.matches(ViewMatchers.withEffectiveVisibility(ViewMatchers.Visibility.VISIBLE)))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment