Test-Driven Development in Android

Jan 24 2023 · Kotlin 1.6, Android 12, AS Bumblebee 2021.1.1

Part 3: Test-Driven Development: UI Tests

18. Test for Text

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 17. Create UI Tests Next episode: 19. Perform an Action

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Transcript: 18. Test for Text

We already have a test that verifies that we have a button that hekps us display a new joke each it is clicked. But we also need to make sure that when the app is just launched a joke is displayed to our user automatically. We need to add a test for that.

Im gonna close the project window. Just below our previous test. First we are gonna create our test joke manually.

Then we need to create our mock repository.

declareMock<Repository>{

whenever(getJoke()){
	.thenReturn(Single.just(joke))
	.onView(withId(R.id.textJoke))
	.check(matches(withText(joke.joke)))
}

}

Then we’llopen our activity_main.xml file, we need to add our textview just below our button we’llcreate our textview.

Just like before dont worry about android studio complaining about not having any constraints. We are jsut worried about having a text view that does not have any text so out test fails.

Execute your test to see it fail. Our test fails as expeted. Lets take a look at the error and scroll down. And here it is. The failt failire handler failed with cause error with text that is doesnt match the selected view.

This error basically says hat we expected our text view to have this particular text with is a text generated by our faker loreimpusemSentence() method. So. lets make our text compile. To do this open your project navigator under jack mainactivity.kt. Scroll down to showJoke(). We already have everything set up so all we need to do is connect our data to our view using our binding.

Go back to your test and see it pass.

Amazing. I’m going to execute all of our tests to make sure all of our code still works.

Amazing!