Mockito lying to you before? Never again!

Eric Wang Gaoxiang
4 min readMay 1, 2020

Welcome back to #ericmonthlywriting on Twitter and #ericmonthlywriting on LinkedIn, 2020 Apr is a pretty busy month for me, therefore instead of new KBP series, I would like to pick an interesting problem I have met during work this month.

Let’s watch Game of Thrones

Game Of Thrones

Even though the problem is about Mockito, to make the article not too dry for reading. I have set up some background for this problem using character inside the popular TV shows.

We have two main class as below:

DaenerysTargaryen Class
GameOfThrones Class

Both classes are very self-descriptive, it tells you that 2 of the dragons have died along the way. As a result, we expected there is only 1 dragon left by following the storyline.

Let’s talk about Mockito

Tasty mocking framework for unit tests in Java

In software development, developers will write test cases to make sure the software logic is correct to avoid bugs. The testing itself is another topic that worth an article about it. In this article, we are talking about the unit test, which is the most fundamental layer of testing.

As your software grows, you may need to rely on 3rd party libraries or data from external API, this makes unit testing harder to achieve. Therefore, most people will mock the behavior of the depending class/data. In the Java/JVM world, Mockito is the framework that most people (including me) like to use to achieve this goal.

The evil side of Mockito

Let’s assume that our DaenerysTargaryen class is one of the dependencies which we need to mock during our test, therefore the test case setup will look like below:

test setup

In the mocked instance, we have changed the dragons into 5 instead of 3. Let’s check out our test results.

test result
Photo by Andre Mouton on Unsplash

The test failed? Why there is only Nana left after the storyline has been called twice? The number of dragons left seems correct. So the root cause must be related to List. The key difference between primitive values and objects is how they are passed in the parameter. Read more about Java way at here.

To check out if the list are really the same reference or not, we may need to make use of System.identityHashCode method to check it out.

log

Ah-ha! It’s indeed the same. Therefore, when you are using Mockito to mock class and thenReturn method, the mocked class is actually keeping the same instance during the test/life of the mock instance.

Takeaways

  1. When using Mockito, be careful with what you are mocking, make sure you don’t make the assumption that mock is the same as the original class.
  2. When your method is taking an object as a parameter, it’s safer to do a defensive copy.
  3. Always write good test cases. :)

The whole project is at (https://github.com/wgx731/wow-mockito) for your reference. I hope you enjoy this month’s reading and have a good holiday and weekend ahead.

As usual, here are the ending lines :)

Follow me on Github, Twitter, Facebook, LinkedIn, Keybase and Medium.

--

--