Mockito Maven Dependency Example

A unit test should test a class in isolation. Side effects from other classes or the system should be eliminated if possible. Mockito lets you write beautiful tests with a clean & simple API. In this example we will learn how to define Mockito dependency in maven and how to use it. Tools and technologies used in this example are Java 1.8, Eclipse Luna 4.4.2

1. Introduction

Mockito is a popular mocking framework which can be used in conjunction with JUnit. Mockito allows us to create and configure mock objects. Using Mockito simplifies the development of tests for classes with external dependencies significantly. We can create the mock objects manually or can use the mocking framewors like Mockito, EasyMock. jMock etc. Mock frameworks allow us to create mock objects at runtime and define their behavior. The classical example for a mock object is a data provider. In production a real database is used, but for testing a mock object simulates the database and ensures that the test conditions are always the same.

Apache Maven is a software project management and comprehension tool. Based on the concept of a project object model (POM), Maven can manage a project’s build, reporting and documentation from a central piece of information. When creating a project in Eclipse, one may use Maven to manage dependencies more easily and to resolve transitive dependencies automatically.

Want to master Mockito ?
Subscribe to our newsletter and download the Mockito Programming Cookbook right now!
In order to get you prepared for your Mockito development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

2. Creating a project

In this section we will see how Eclipse can help us create a simple maven project. Below are the steps we need to take to create the project.

Figure 1. Create Maven Project
Figure 2. New Maven Project
Figure 3. Configure Project
Figure 4. Maven Project Structure

Open the pom.xml file to view the structure Maven has set up. In this file, you can see the information entered in the steps above. You may also use the tabs at the bottom of the window to change to view Dependencies, the Dependency Hierarchy, the Effective POM, and the raw xml code for the pom file in the pom.xml tab.

Figure 5. POM

3. Adding dependencies

Dependencies can be added in two ways. Either directly specifying the dependencies in the pom.xml tab or using ‘Dependencies’ tab to add dependencies. We will use the later.

Open the pom.xml file and click on the ‘Dependencies’ tab. Click on the ‘Add…’ button. Eclipse will open a popup where you can define dependencies. Enter the details as below:

Group Id: org.mockito

Artifact Id: mockito-all

Version: 1.9.5

Figure 6. Select dependency

Click OK. Check the pom.xml file. Eclipse will add the below section:


Repeat the same steps to add the JUnit dependency

Group Id: junit

Artifact Id: junit

Version: 4.12

Now our final pom will look like below:

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  <name>Mockito Maven Dependency</name>
  <description>Example of Mockito Maven Dependency</description>

4. Testing

Now we will test if our maven project has been set up correctly or not. We will create a simple test class to test this.

package mockito;
import java.util.List;
import org.junit.Test;
import static org.mockito.Mockito.*;
import static org.junit.Assert.*;
public class MockitoExample {
  public void test() {
    List<String> mockList = mock(List.class);
    assertEquals("Mockito", mockList.get(0));
    assertEquals("JCG", mockList.get(1));

Run this class as JUnit test and it should run successfully. This will prove that your dependencies are setup correctly.

5. Download the source file

In this example we saw how to setup a maven dependency for Mockito using Eclipse

You can download the full source code of this example here: Mockito Maven Dependency
Exit mobile version