Unit testing with Mockito

As we discussed in the previous recipe, we cannot use Android components in unit tests. This is why we are able to run them faster, and without any device. If you want to use Android components in your tests, there are two options:

  • Write integration tests, which run on your device or emulator.
  • Use a mocking framework, such as Mockito, which basically mocks the Android SDK components so that you can use them without any device or emulator, just like any other unit test. The benefit of a mocking framework is that it takes a lot less time to run the tests, as the tests are basically unit tests only. Here's an accurate definition of a mock object by Vogella:
"A mock object is a dummy implementation for an interface ...

Get Kotlin Programming Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.