Using Mockito For Beginners

Stack: JUnit 4, Mockito 2, Java SE 7

To start off, this article will talk about the basic features of getting around using Mockito with JUnit.

In this scenario the class Sample has two basic methods that will be used in order to demonstrate Mockito’s abilities.

The code bellow shows how the methods are built and what their purpose is.

The SampleTest class tests the two methods above by using Mockito.when and Mockito.doReturn in different ways.

As see above, when a class has been created through Mockito.spy the real methods of that class will be called unless they are mocked with Mockito.when. However, it is not always the case that the mocked method will be called since there scenarios that will trigger the real method… And so, what is thought as mocked sometimes isn’t.

Show your support

Clapping shows how much you appreciated Carlos Pinto’s story.