Test-Driven Development in Android

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

Part 1: Unit Tests

05. Create Unit 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: 04. Set Up JUnit Next episode: 06. Create More 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: 05. Create Unit Tests

Inside your GameUnitTests class create your first test and we are going to name this test:

fun whenIncrementingScore_shouldIncrementCurrentScore

Remember that your test should be very specific and should convey what you are trying to test with this method. In this case we are trying to test that when incrementing our score we should also increment the current score variable of the game class.

We are going to add the @Test annotation. This basically says that this is a junit test that can be run for unit testing purposes. Now type:

val game = Game

We are creating a new instance of the Game class, dont worry if this doesnt compile yet, we have not created this class yet thats why we are getting an error. Remember that when using test driven development we create the unit test first and then the functionality to make this test pass.

Then type

game.incrementScore()

Assert.assertEquals()

import missing dependencies. Import the first one from the org.junit package. In here type:

1, game.curentScore.

So, what happened here?

First we created an new instance of a class that we have not yet created, the Game class. Then we said that we are going to call the incrementScore method of the game class.

And finally we are going to test that after calling this method we want the current score property of this game instance to be one. This assertion is going to verify is that this expected value is what we get here, if not the test will fail.

There is also an optional parameter to write a message so that in case the test fails you will see that specific message. For example we can say something like current score should have been one.

You will soon see that every step has essentially two steps. First we have the set up on which we create the instances that we are going to test and then we have the assertion which is when we execute a method and verify that its result is what we expect.

If we try compile this we will see that test fails because there no such class Game yet so we need to make the test compile. Remember this is test driven development, we first want to see our test fail and then we need to make them pass.

So open your project navigator scroll up and under your main package/java inside ‘cocktails’ create a new game package. Inside game we are going to create another package: module.

Inside our module package we are going to create a new class and we are going to name it Game.

Now we want to write the minium ammount of code to make our test compile, just the bare minium:

Var currentScore = 0
private set
fun incrementScore(){}

This is the bare minimun code to make our test compile. If we compile it what do you think will happen?

And the test failed, why? Because we have not yet added the functionality to increment the score so this assertion returns false. In this window you will see the results of your tests. Here we receive the message that we typed here: current score should have been 1 expected 0.

The test failed, nice! This is something to celebrate when we are doing test driven development dont worry. Oh, by the way you may have seen that to execute our test I did it simply by clicking on this button. You can also use the short cut that is here, I normally dont use it I prefer to just click here to execute my unit tests. If you have several unit tests on a single class, you can click on this button right next to your class.

Right now the results are the same since we only have one test but if you had many you may see something like 2 out of 2, 3 out of 3, 2 out of 3, or 1000 out of 1000. You can have many many unit tests.

So, anyway your test didnt pass, we need to fix that.

Go back to your Game class and in here to make our test pass we simply need to type:

currentScore++

Execute your tests.

Im going to use this button now so you guys can see it is exactly the same thing.

And nice we receive a nice message test passed 1 out of 1. And it only took us 1 ms.

There is only last thing i want to show you in this episode. Open your project panel, you project window and open the app package, build, report, and right click on this: select open in browser. Im going to use safari. And this is a nice report that is generated about the tests that we just ran. You will see a nice report in html about the percentage of tests that were executed and were succesful. You will see that we executed one test. There were 0 failures, 0 ignored and the duration was 0.001 seconds. Success rate is 100% thats exactly what we want.

I’m going to close my browser.

If you are eager to create more tests don’t worry you will create much more of them in the following episodes. Amazing!