Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@grumpyshoe
Created January 18, 2019 09:52
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 grumpyshoe/d94920900bdd2d49f05233059c2c6643 to your computer and use it in GitHub Desktop.
Save grumpyshoe/d94920900bdd2d49f05233059c2c6643 to your computer and use it in GitHub Desktop.
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