Day 8: Repository, tested
Rahul Chowdhury 🕶
Posted on July 31, 2019
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 mockingBoolean
values. Turns out there was an issue with Mockito v2.8.9 while unboxing/autoboxingBoolean
values on Kotlin. Solved it by upgrading to v3.0.0.IllegalStateException
becauseArgumentMatchers.eq()
returns aNullable
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.
Posted on July 31, 2019
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.