Create your first unit test

We all see that everybody talks about and praising importance of unit tests, but when it comes to you, try it for the first time, you might have hard time to understand it, right?

We are now going to improve the most popular unit test in Android history!

@Test
fun `addition_isCorrect`() {
assertEquals(4, 2 + 2)
}

Well, I’m feeling that this buddy is still alive in many many repositories.

Note: Since I am going to talk about the basics and mostly the idea of testing, the libraries do not need much attention.

Basics

First things first, you need certain rules to write a decent test because you can be tired, be in a hurry or you don’t even want to write unit tests. In those cases it is a matter of time that you will end up in a frustration and at this point rules come to play to save your butt.

1- Create your test format

This is one of the best ways to keep everything clean and help you where to start.

@Test
fun `blabla`() {
// Given

// When

// Then
}

2- Finding your expectation(not expectations, this is a key point)

Let’s talk about this because this one is also very crucial. Just hold on a second, and find your expectation from this test. Let’s make a small brainstorming together.

I want.. my test.. to find.. the result of.. sum..

We now know the meaning of the life of the test, and refactoring the function name while typing first line of code.

@Test
fun `sum is expected`() {
// Given

// When

// Then
assertEquals(expected, result)

}

So, here we are with a test that cannot even compile. But don’t stop, keep going, now find the improved expectation.

I want.. assertion success.. when.. sum is executed..

@Test
fun `sum is expected`() {
// Given

// When
val result = sum(param1, param2)

// Then
assertEquals(expected, result)

}

We edited it again, and do not forget that we still have no Calculator class or sum function.

@Test
fun `sum is expected`() {
// Given
val param1 = 2
val param2 = 2
val calculator = Calculator()

// When
val result = calculator.sum(param1, param2)

// Then
assertEquals(4, result)
}

And it’s done, we have first implemented the test then code, and it seems decent.

3-Use other libraries when needed

In reality, your app depends of a lot to 3rd parties. The most possible ones that RxJava, Coroutines, LiveData, Room etc.

These libraries won’t work standalone like shown above, you will need their custom rules or runners, fortunately they are all provided by the owners or community.

In testing world, there are runners to decide how your tests will be executed. For example JunitRunner, BlockingJunitRunner etc. You can also create your custom runners or add rules to other runners.(eg: InstantTaskExecutorRule for LiveData testing).

4- Find what your objects will be? a Mock, Stub or Dummy

Sometimes you really need help from libraries, and one of the bests are Mocking ones. So what is mock, dummy and stub?

Mock; objects are not real however you can do whatever you want with them by getting wonderful features of mock libraries. Especially deciding return values, making construction is easy, or if the function have to be called or not called.

Do not forget that mocking objects are expensive. I have seen many times that people were using mocks but they were able to create simple and better tests by asserting(first test we created). Use Assertions over Mocks when it is possible.

Stub; Assume that we have an abstract class or interface and don’t really care input or output, so you just create a subclass and test the implementation or basic functionality.

Dummy; Similar to stubs but you create dummies with caring data and we already do it many many times.

Conclusion

I have 2 recommendations, It is likely to test your code by mocking or asserting, and you better be trying assertions first because they are highly performant.

And second one is a simple readout from android;

Thank you!

Lead Android Developer currently at Adesso Turkey, Retired Indie Game Developer, Amateur endurance cyclist

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store