Test-Driven Development in Android

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

Part 1: Unit Tests

08. Answer Question Tests

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: 07. Create JUnit Annotations Next episode: 09. Refactor JUnit Tests

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: 08. Answer Question Tests

We also will need to know if a question was answered correctly. So now we’re going to make the answere method return a boolean. The result will be true when the answer is correct and false when the answer is incorrect.

We’re going to add one more test:

@Test
fun whenAnswering_withCorrectOption_shouldReturnTrue(){
    val question = Question("CORRECT", "INCORRECT")
    val result = question.answer("CORRECT")
    Assert.assertTrue(result)
}

Executing this test will get you a compilation error because the answer method doesn’t return a boolean so go back:

fun answer(option: String): Boolean{
answeredOption = option
return false
}

This will make your test compile for now. Run it and watch it fail.

We’re going to temporary fix this method to return true to make our test pass. Nice.

So, we’re going to do the opposite we’re going to verify that when answering incorrectly we return a boolean equals to false. Create a new test:

@Test
fun whenAnswering_withInCorrectOption_shouldReturnTrue(){
    val question = Question("CORRECT", "INCORRECT")
    val result = question.answer("CORRECT")
    Assert.assertTrue(result)
}

We have the same question object here, it is the same as the one right here. The difference is that we’re now assigning the result variable to the question return value of INCORRECT which should be false. Execute your test, see it fail.

And now to make both of our tests pass go back. And in here instead of just returning a true or false value we’re going to return correctOption == answeredOption

Execute all of your tests ans see them pass. Four out of four. Amazing!!!