Test-Driven Development in Android

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

Part 1: Unit Tests

06. Create More 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: 05. Create Unit Tests Next episode: 07. Create JUnit Annotations

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: 06. Create More Tests

Close your project window.

And we are going to create more tests from our Game class.

This is a very important class since it is a like a main point of entry for all the other classes that we will be creating in the next episodes. The viewmodels, the score class, all of them. So, we need to create a good amount of tests to be sure that it is a good class a fine class.

Im gonna add more space so you guys can see properly.

My editor always tells me to add more space at teh bottom so you guys can see what im typing. i must not forget that, otherwise I dont get paid.

The game needs to show the highest score so we need to add a test that checks that when the current score is above the highest score it increments the highest score. So, create a new tests and we are going to name it

fun whenIncrementingScore_aboveHighscore_shouldAlsoIncrementHighScore

Don’t worry about creating long names for your tests. Remember that your tests should be very specific, and have good names that tell eveyone what they are testing. Never forget to add the @Test annotation.

val game = Game() 

Remember the two steps that I told you in the previous episode. First is the set up where we create our mocks and class instances. And now we execute our methods and create assertions or verifications that they return what is expected.

game.incrementScore() 
Assert.assertEquals(1, game.highestScore)

Easy enough right. It is very similar to the test that we crated in the previou episode. If we execute our test it wont even compile because we dont have a property named highest score in our class.

Lets create it now:

var highestScore = 0
private set

We wrote the bare minimum code to make our test ocmpile. Let’s see it fail and it failed.

By the way if you execute all your tests using this button here you will see tests failed 1, passed 1 of 2 tests.

Whatever you choose if up to you but if you modify your code it is always advisable that you execute all of your tests to make sure that they all still pass.

For educational purpose I just click this button since it is easier and faster but again when adding new code to your classes make sure all of your tests pass.

Lets make our test pass. Go back to your game class. And we are going to say:

highestScore++

If we execute all of our tests we will see that all our tests pass.

Nice!