Initialize Mockito Tests with JUnit

This example show you how to initialize mockito tests using annotations. First we can use the MockitoAnnotations.initMocks(this) method to explicitly initialize the annotated mocks. The MockitoJUnitRunner JUnit runner uses the MockitoAnnotations.initMocks(this) method to initialize the annotated mocks.

Dependencies

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>com.memorynotfound.test.mockito</groupId>
    <artifactId>initialize-tests</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>MOCKITO - ${project.artifactId}</name>
    <url>http://memorynotfound.com</url>

    <properties>
        <mockito.version>1.10.19</mockito.version>
        <junit.version>4.12</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-all</artifactId>
            <version>${mockito.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

Initialize Mockito Tests with MockitoAnnotations

The MockitoAnnotations.initMocks(this) method is called in the @Before method. This method initializes the annotated mocks.

package com.memorynotfound.test;

import org.junit.Before;
import org.junit.Test;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;

public class InitializeBeforeTest {

    @Spy
    List<String> messages = new ArrayList<String>();

    @Before
    public void setUp(){
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testSpyOnList()  {
        String message = "Initialize Mockito Tests with MockitoAnnotations";
        doReturn(message).when(messages).get(13);
        assertEquals(message, messages.get(13));
    }

}

Initialize Mockito Tests with MockitoJUnitRunner

When using JUnit we can also register a MockitoJUnitRunner to the class. This method calls the MockitoAnnotations.initMocks(this) method to initialize the annotated mocks.

package com.memorynotfound.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import java.util.ArrayList;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.doReturn;

@RunWith(MockitoJUnitRunner.class)
public class InitializeJUnitRunnerTest {

    @Spy
    List<String> messages = new ArrayList<String>();

    @Test
    public void testSpyOnList()  {
        String message = "Initialize Mockito Tests with MockitoJUnitRunner";
        doReturn(message).when(messages).get(74);
        assertEquals(message, messages.get(74));
    }

}

References

You may also like...