DEV Community

Rahul Chowdhury 🕶
Rahul Chowdhury 🕶

Posted on

Day 8: Repository, tested

Yesterday's work

I started refactoring my repository to accept local and remote source contracts instead of concrete classes.

This move was to make my repository unit-testable using JUnit4 and Mockito.

Here's an awesome tutorial on how to use Mockito for unit tests: https://www.vogella.com/tutorials/Mockito/article.html

Today?

I worked the morning on writing unit tests for my repository. I have to say, it was fun.

I used Mockito for the first time and it was a great experience. There were hiccups here and there but overall it was a nice experience.

Some of the roadblocks I faced:

  • NullPointerException on mocking Boolean values. Turns out there was an issue with Mockito v2.8.9 while unboxing/autoboxing Boolean values on Kotlin. Solved it by upgrading to v3.0.0.

  • IllegalStateException because ArgumentMatchers.eq() returns a Nullable value and my parameters were non-nullable. I had to fix this by introducing a local alternate method like this:

fun <T : Any> safeArgumentEq(value: T): T = eq(value) ?: value

My repository is now well-tested. You can find the test file for reference, on GitHub.

Top comments (0)