Home » Java » Enterprise Java » JUnit and Mockito cooperation

About Alexey Zvolinskiy

Alexey Zvolinskiy
Alexey is a test developer with solid experience in automation of web-applications using Java, TestNG and Selenium. He is so much into QA that even after work he provides training courses for junior QA engineers.

JUnit and Mockito cooperation

This time I want to make an overview of testing framework Mockito. Definitely this is one of the most popular tools for the testing of java code. I have already made the overview of the Mockito’s competitor – EasyMock. This post will be based on the sample application from the post about EasyMock. I mean classes which represent coffee machine functionality.

Preparation of testing with Mockito

As usually I will use Maven for the project setup. So to make Mockito available in the project, I need to add following dependency in the pom.xml file:


Notice that JUnit isn’t required for the normal functionality of Mockito, you can use in the same way TestNG instead of it. So you can use either JUnit or TestNG depending on your decision.

Let’s move further. As I mentioned earlier, all tests in this article will be written for the sample application from the post about EasyMock. You can read just “Application code” section from there. That will be enough to understand the subject of the testing.

Mockito & JUnit tests

I want to start this section with a couple of sentences about the Mockito framework. Definitely with the help of Mockito you can do everything you wish in the testing. You can create mocks, stubs, make verifications and all this with the help of clear API.
It’s time to see how unit tests with Mockito look like.

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.app.data.Portion;
import com.app.exceptions.NotEnoughException;
import com.app.mechanism.CoffeeMachine;
import com.app.mechanism.interfaces.ICoffeeMachine;
import com.app.mechanism.interfaces.IContainer;

public class CoffeeMachineTest {

	ICoffeeMachine coffeeMachine;

	IContainer coffeeContainer;
	IContainer waterContainer;

	public void setUp() {
		coffeeMachine = new CoffeeMachine(coffeeContainer, waterContainer);

	public void tearDown() {
		coffeeContainer = null;
		waterContainer = null;
		coffeeMachine = null;		

	public void testMakeCoffe() throws NotEnoughException {



	public void testNotEnoughException() throws NotEnoughException {





I will explain in a few words, what’s going on in the test class. I’m testing the coffee machine and for this purpose I create mocks for the coffee container and water container. Further in the tests I will define some behavior for the mocks, and depending on this coffee machine will perform expected actions.

	IContainer coffeeContainer;
	IContainer waterContainer;

In the code above I declared two mocks. Creation of the mocks in this way requires @RunWith(MockitoJUnitRunner.class) annotation applied to the test class. As the alternative you can use MockitoAnnotations.initMocks(testClass); before any test will run, e.g. you can put this code snippet in the method annotated with @Before.


Defining mock’s behavior in the Mockito performs in a very convenient manner. You specify what should be returned after the particular method was called. Notice that I have made several static imports in the test class.


Mockito framework has conquered my heart. It’s very convenient, its API is clear, usage is laconic. Mockito contains a lot of methods which can be needed during the unit tests development. Of course one post is too small to describe all features of Mockito, so I recommend you to dig deeper using follow link to the official documentation.

Reference: JUnit and Mockito cooperation from our JCG partner Alexey Zvolinskiy at the Fruzenshtein’s notes blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....


One comment

  1. Very useful article. A good introduction to Mockito, for a newcomer like me.

Leave a Reply

Your email address will not be published. Required fields are marked *


Want to take your Java Skills to the next level?
Grab our programming books for FREE!
  • Save time by leveraging our field-tested solutions to common problems.
  • The books cover a wide range of topics, from JPA and JUnit, to JMeter and Android.
  • Each book comes as a standalone guide (with source code provided), so that you use it as reference.
Last Step ...

Where should we send the free eBooks?

Good Work!
To download the books, please verify your email address by following the instructions found on the email we just sent you.