DEV Community

Rahul Chowdhury 🕶
Rahul Chowdhury 🕶

Posted on

2 1

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.

Sentry blog image

The Visual Studio App Center’s retiring

But sadly….you’re not. See how to make the switch to Sentry for all your crash reporting needs.

Read more

Top comments (0)

Billboard image

Deploy and scale your apps on AWS and GCP with a world class developer experience

Coherence makes it easy to set up and maintain cloud infrastructure. Harness the extensibility, compliance and cost efficiency of the cloud.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay