Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example UI-Test
class MainViewTest {
@Rule
@JvmField
var testRule = IntentsTestRule(MainActivity::class.java, true, false)
// nedded for correct handling of 'postValue' in LiveData
@Rule
@JvmField
var rule: TestRule = InstantTaskExecutorRule()
@Before
fun setUp() {
// reset expectations so it's clean on new test
MockApiService.resetExpectation()
}
@Test
fun request_data___success___response_is_shown() {
// define test data
val myArray = JSONArray()
myArray.put("the test response data")
// set expectation
MockApiService.setExpectation(MockApiService.Method.getData, Expectation(success = { MockSuccess(myArray) }))
// launch activity
testRule.launchActivity(null)
// check assertions
"the test response data".checkIsDisplayedAsText()
}
@Test
fun request_data___failure___no_data_text_is_shown() {
// set expectation
MockApiService.setExpectation(MockApiService.Method.getData, Expectation(failure = { MockFailure("some error") }))
// launch activity
testRule.launchActivity(null)
// check assertions
"- no data -".checkIsDisplayedAsText()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment