Test-Driven Development in Android

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

Part 1: Unit Tests

07. Create JUnit Annotations

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: 06. Create More Tests Next episode: 08. Answer Question 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: 07. Create JUnit Annotations

For this project you are creating a trivia game. Trivias have questions right?

So you will now create unit tests that model a question with two possible answers.

This question also has an answered option to model what the user has answered to the question.

So let’s create the unit tests as usual.

Open the app source test package. Under model we are going to create a new kotlin class file: QuestionUnitTests.

I’m going to close the Game class and the GameUnitTests class. I’m going to close the project window for now.

We are going to add the following code:

@Test
fun whenCreatingQuestion_shouldNotHaveAnsweredOption(){
    val question = Question("CORRECT", "INCORRECT")
    Assert.assertNotNull(question.answeredOption)
}

This is just a new test. Let me add the appropiate dependencies. And here we simply use the AssertNull to check if the question.answered option is equal to null. If you try to execute this test it wont compile because the Question class does not exist yet. So we need to add the minimum code to make it compile.

Go to your project window. Open the main package java, inside the Game model package create a new kotlin class file and we are going to name it as you might guess Question. The first one is going to be the correctOption property of type String and the second one is going to be the incorrectOptionProperty val incorrectOption of type String.

var answeredOption: String = "MY_ANSWER"
private set

Execute your test again and see it fail. And it failed because we hard coded the MY_ANSWER option which is not null so modify the Question class to make it equals null. Go back to your tests and see them pass.

We can add another tests to our QuestionUnitTests class. I am going to add some more space here so you guys can see properly:

fun whenAnswering_shouldHaveAnsweredOption(){
}

This test will check that when you add the user’s answer to a question the user’s answer is saved to the answer property right here.

@Test
val question 

Remember your two important steps of creating a unit test, first we do the set up were we create our objects and mocks which you will learn how to use in the next section:

val question = Question("CORRECT", "INCORRECT")
question.answer("INCORRECT")

Then we are going to do the assertion.

Assert.assertEquals("INCORRECT", question.answeredOption)

We get a compilation error because we dont yet have this method. As usual go back to your Question class and create a new method

fun answer(){
} 

with the bare minimum implementation to make our test compile.

Oh I forgot to add the option parameter of type String.

Go back to your Question and in here answeredOption = option

Execute your tests and see them pass.

So what did we do with these tests?

Easy we simply created a new question object with two different options the correct option is CORRECT and the incorrect option is INCORRECT. Then we called the answer method of the Question object with the option equals to INCORRECT.

This method simply assigns the answered option to be equals to the option passed as a parameter and then we are asserting that the expected value of this property is INCORRECT which it is.

Simple right?