Unit Tests in Kotlin

Hello guys, welcome to this weeks post. Today we are going to learn how to do Unit Tests in Kotlin. Of course, Kotlin also allows us to do unit tests in a very simple way, and very similar to what we’re used in Java.

There are some small complications when we use libraries like Mockito, but we’ll see some tricks to make it easier.

Unit tests in Kotlin

Although the subject of what a unitary test is always generates controversy, I won’t go into details here about this topic.

For our particular example, it’s enough to think that a unit test is a test that doesn’t need a device to run. The IDE will be able to execute them and display a result, identifying which ones were executed and which ones failed.

Configure Gradle

You need to add jUnit to your dependencies. It’s possible that, when you created the project, it was already be included by default. We’ll also add Mockito, as we’ll use it later:

Create your first test

In the app/src/test folder (you can create it if it doesn’t exist yet), you can create a new class called MyTest, which looks like this:

As you can see, it’s very similar to what you’re used to in Java.

How to use Mockito

Mockito in Kotlin can be used like any other library, although it’s true that you could find some complications that you’ll need to solve.

Here is an example

You see, everything is very similar. You can create your mocks and use them seamlessly throughout the code. Although I’m not doing it here, you could also use ‘MockitoJUnitRunner’ and annotations.

The word when is a reserved word in Kotlin, so you need to use inverted commas, or you could even rename the import and give it the name you want:

The problem arises when trying to mock types that don’t allow null values. Mockito by default gives null values to mocked objects, so sooner or later problems will arise.

A little trick is to use a library like mockito-kotlin, which instead of using nulls, will give specific values by default to each type, solving that problem. In addition, it provides other functions that take advantage of Kotlin’s power to make things simpler.

Another problem is that, by default, all classes and functions in Kotlin are closed, which means that they can’t be extended. This is a problem for Mockito, which won’t be able to mock them.

But that’s not an issue anymore, because Mockito 2 allows mocking final objects.

In an upcoming article we’ll see how to use it.

A little interesting thing

Kotlin allows us more flexibility than Java by naming functions. If we use inverted commas, we can put any text that comes to our mind.

This can be very useful for tests, where the most important thing is that the name of the test perfectly describes what you are doing, so as to be able to serve as specification.

Therefore, you can have a test that is named like this:

The best thing is that, apart from improving readability, it is also better understood in the output of the result when it fails. You’ll see the readable error more clearly.

If you use it on an Android project, you’ll see that it shows a Lint error indicating that methods on Android projects can’t have spaces. In my tests I haven’t noticed that this can be a problem. Gradle runs them without problem, so you can add an annotation to ignore the error.

In any case, remember to use this only for tests.


Although theoretically the testing tools that we use in Java should work without problems in Kotlin, it’s true that those that are based on reflection and add null to the code can give us problems.

Kotlin is very careful about nullity, and this can be a sticking point in some cases. But increasingly, there are more alternatives to do it in a simple way, and with Mockito 2 all those problems tend to disappear.

Apart from these small pitfalls, everything else works just as it would in Java.

Want to keep learning about Kotlin? Just check all the articles I’ve been writing about the topic.

Happy Coding!

Juma Allan

In this blog, I write tutorials and articles related to Android and Node Js coding. I am an Open Source Enthusiast and have been programming Android for three years now. I love sharing my knowledge with others, and learning new languages everyday. I write on this blog to share what I've gained by learning from the web and friends.