Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Importance of Being Tested

The Importance of Being Tested

Slides of the talk I gave at Droidcon Berlin 2021

Roberto Orgiu

October 21, 2021
Tweet

More Decks by Roberto Orgiu

Other Decks in Programming

Transcript

  1. My take on integration testing •No Android deps •Test fl

    ow from start to end •Use Robolectric
  2. class RootFragment : Fragment() { lateinit var repository: Repositor y

    lateinit var view: Vie w fun onResume() { val response = repository.fetchThings( ) view.bindResults(response ) } }
  3. class IntegrationTest { private val mock = TestDouble(Service() ) private

    val testFragment = RootFragment( ) fun integrationTest() { run(testFragment).verify( ) } } androidTest
  4. What about network? • Retro fi t + OkHttp +

    MockWebServer • Ktor + MockEngine
  5. val mockEngine = MockEngine { request - > respond (

    content = ByteReadChannel(content) , status = HttpStatusCode.OK , headers = headersOf(ContentType, type ) ) }
  6. @get:Rule var activityScenarioRule = activityScenarioRule<MyActivity>( ) @Tes t fun changeText()

    { onView(withId(startViewId) ) .perform ( typeText(MESSAGE) , closeSoftKeyboard( ) ) onView(withId(buttonViewId) ) .perform(click() ) onView(withId(targetView) ) .check(matches(withText(MESSAGE)) ) }
  7. @Composabl e fun SimpleUI() { var clicks by remember {

    mutableStateOf(0) } Column { Button ( onClick = { clicks++ } ) { Text(text = "Click me" ) } if (clicks > 0) { Text(text = "$clicks" ) } } }
  8. @get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun

    verify_initial_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithTag("clicks" ) .assertDoesNotExist( ) }
  9. @get:Rul e val composeTestRule = createComposeRule( ) @Tes t fun

    verify_last_case() { composeTestRule.setContent { SimpleUI( ) } composeTestRule.onNodeWithText("Click me" ) .performClick( ) with(composeTestRule.onNodeWithTag("clicks")) { assertIsDisplayed( ) assertTextEquals("1" ) } }