Using Mockito with JUnit


In this lesson with Mockito, we will use another framework for complete testing, known as JUnit. According to JUnit website,

JUnit is a simple framework to write repeatable tests. It is an instance of the xUnit architecture for unit testing frameworks.

With Mockito, creating mock objects is very easy. It provides many simple annotations to do so. Internally, a mock is nothing but a proxy for the actual class instance.

Here are steps to create Mockito JUnit example.

Step 1: Create a simple java maven project.

Maven Dependency

Step 2: Add required dependencies to pom.xml

A Maven dependency is the fastest way to get started with Mockito:

Just a single dependency which doesn’t bring any other libraries with it. See here for latest versions of the library.

As we will also be using some JUnit functionality, we will also need it’s dependency. Let’s add it next,

Finally, we will also be using AssertJ.

Your pom.xml will look as below:

Mockito and JUnit

The simplest way to get started easily is by showing an example.
Step 3: We will define an interface named as MathService. Here is our interface:

Next, we will be adding its implementation. Clear enough, those will be simple mathematical implementations:

What to do next?

Step 4: Let us focus on the steps we need to perform to get started with Mockito mocks:

  1. Annotate the test class with @RunWith(MockitoJUnitRunner.class).
  2. Annotate the test fields with either @Mock or @Spy annotation to have either a mock or spy object instantiated.
  3. Annotate the system under test with @InjectMocks annotation.

Let’s apply above three steps to provide our test demo.

Now, it is worth noticing that when a Mock is used, the actual method implementation is not called. In the second test-case we wrote, the implementation of the method was actually called.
When you run above test, you will get below output:
JUnit Result Sometimes, the test case classes are already been annotated with a @RunWith annotation and Mockito’s annotation won’t have any effect. In order to correct this, you have to call MockitoAnnotations.initMocks manually like,

Mockito vs JUnit

JUnit is a framework that helps with writing and running your unit tests.

Mockito (or any other mocking tool) is a framework that you specifically use to efficiently write certain kind of tests.

The main objective to perform unit testing to isolate your class you want to test from anything else in your project and testing its functionality. To do that, we have to create “test doubles” that you provide to an object of your “class under test”. We can create all those “test doubles” ourselves too, or you use a mocking framework that generates an object of a certain class for you using reflection techniques. Interestingly enough, some people ask to never use mocking frameworks but without them, we will be just reinventing the wheel, a bad one.

In other words: you can definitely use JUnit without using a mocking framework. Same is true for the reverse direction; but in reality, there are not many good reasons why you would want to use Mockito for anything else but unit testing.

@RunWith(MockitoJUnitRunner.class) vs MockitoAnnotations.initMocks(this)

Using MockitoJUnitRunner rather than JUnitRunner is really optional.

The main advantage provided byMockitoJUnitRunner is relieving you to explicitly invoke MockitoAnnotations.initMocks(Object) when you use the @Mock way to create your mocks.

But you could also get a few more misuse reports of the Mockito framework by using this runner that may be missing without using it.

Example of incorrect uses that may be caught out of the box without using the MockitoJUnitRunner:

But these could not be caught in all cases.


Add Comment