Android unit testing with Mockito and Kluent

In my previous article, I explained how we could build an Android App using Clean Architecture. In this article, I am going to show how to write unit tests for that project using Mockito and Kluent.

Kluent is a “Fluent Assertions” library using the Infix-Notations and Extension Functions of Kotlin to provide a fluent wrapper around the JUnit-Asserts and Mockito. I like Kluent for the reason that it makes my test classes more readable, and it includes a lot of functions to make unit testing easy.

Let’s start by adding dependencies:

testImplementation 'org.amshove.kluent:kluent-android:1.61'
testImplementation 'org.amshove.kluent:kluent:1.61'
testImplementation 'org.mockito:mockito-core:2.23.0'

Here is the implementation of PostRepository

To write tests for the PostRepositoryImpl, we will have to stub the ServiceProvider and the PostService.

The first test is to verify that getPosts() returns Result.Success. Let’s stub PostService and ServiceProvider. Since ServiceProvider is a dependency of PostRepositoryImpl, we will mock it.

private val serviceProvider: ServiceProvider = mock()

Here is our first test in :

Line 4, we mock PostService. Line 2, we define a CompletableDeferred of a list of PostDAO. Line 6, we set the expected Success response.

Line 7, we stub the getPostService() method to return postService. That means we are telling the ServiceProvider to return postService when we execute getPostService().

Line 8, we stub the getPostAsync() method to return postResponse. That means, we are telling the PostService to return postResponse when getPostAsync() is executed.

Line 9, we verify that we call getPostAsync().

Line 10, we assert that expected should be equal to actual.

All this is wrapped into a runBlocking{} block because we are testing a suspend function.

Also, we should test the mapToPost() extension function to trust it behaves correctly in the PostRepositoryImpl class.

The second test is to verify that getPost throws an error.

Line 5, we stub getPostsAsync() to throw an Exception when called.
Line 1, we mention that we should expect an Exception.

Finally, this is how PostRepositoryImplTest.kt looks like:

Here is the implementation of PostInteractor:

Here is PostInteractorImplTest class.

In the first test, we verify that we invoke the getPosts() method of postRepository.

The second test is to show that when calling getPosts(), it returns an error. For that, I stub the getPost() method of the PostRepository to return an error (Line 18).

The third test is to show that when calling getPosts(), it returns a list of Posts. For that, I stub the getPost() method of the PostRepository to return Success(Line 28).

We are only going to write tests for the getPostAsync() method. Here is the Test class:

In the first test method, we verify that the showProgress() and hideProgress() methods are called (Line 17, 18).

The second test method is to verify that the setPost() method of the view is called when the postInteractor call is successful (Line 28).

The third test method is to verify that the showMessage() method of the view is called when the postInteractor call is not successful (Line 37).

Also, we should test the mapToPostItem() extension function to trust it behaves correctly in the PostPresenter class.

That is all. Here is the full code:

If you liked the article, please clap it. Thanks !!

Lead Android Developer @ VMLY&R