How to Mock using Mockito

In this example, we will demonstrate how to mock using mockito. This is a simple demonstration on how to use Mockito to mock external classes in a JUnit test. When doing unit testing, you are testing only the logical algorithm of the class. It should not be dependent to other class or service. That’s why we mock the external service, or other dependency. You should know the difference between a unit test and an integration test.

For a start, add the dependency of Mockito to your project.

Consider the MyClass to be the class under test.

package com.javapointers;

public class MyClass {

    public boolean isNameKey(Model model) {
        return model.getKey().equals("name");
    }

    public String getAttributeString(Model model) {
        if (model.getAttribute().equals("primary")) {
            return "is primary";
        } else {
            return "invalid attribute";
        }
    }
}

class Model {
    String key;
    String attribute;

    public Model(String key, String attribute) {
        this.key = key;
        this.attribute = attribute;
    }

    public String getKey() {
        return key;
    }

    public String getAttribute() {
        return attribute;
    }
}


And the test class for MyClass is MyClassTest. In our test class, we use annotations of Mockito, and we need to initialize the annotation by running the unit test with MockitoJUnitRunner.class or we use MockitoAnnotations.initMocks(this). By using annotation @Mock, we define the object to be a Mock Object, thus we should define how the object will respond when its public methods were called. Below is the code of MyClassTest.java

package com.javapointers;

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

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

@RunWith(MockitoJUnitRunner.class)
public class MyClassTest {

    @Mock
    private Model mockModel;

    private MyClass myClass;

    @Before
    public void setUp() throws Exception {
        myClass = new MyClass();
    }

    @Test
    public void isNameKey_modelKeyIsName() {
        when(mockModel.getKey()).thenReturn("name");

        boolean isNameKey = myClass.isNameKey(mockModel);
        assertTrue(isNameKey);
    }

    @Test
    public void isNameKey_modelKeyIsNotName() {
        when(mockModel.getKey()).thenReturn("something");

        boolean isNameKey = myClass.isNameKey(mockModel);
        assertFalse(isNameKey);
    }

    @Test
    public void getAttributeString_attributeIsPrimary() {
        when(mockModel.getAttribute()).thenReturn("primary");

        String attributeString = myClass.getAttributeString(mockModel);
        assertEquals("is primary", attributeString);
    }

    @Test
    public void getAttributeString_attributeIsNotPrimary() {
        when(mockModel.getAttribute()).thenReturn("secondary");

        String attributeString = myClass.getAttributeString(mockModel);
        assertEquals("invalid attribute", attributeString);
    }
}

We stub the mock object by using when and thenReturn methods. Thus we can create different scenario when a certain object was passed to our class.

Share this tutorial!