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