Mockito Deep Stubs Example

This tutorial tries to explain how to use Mockito’s Deep Stubs feature.

Other testing tutorials:
How to mock static, private methods (Powermock tutorial)
Unit testing void methods using verify and ArgumentCaptor
Mockito Deep Stubs Example
Selenium based automated test using Java, JUNIT and Maven – Example

When you’ve situations where your class under test has multiple levels of dependencies, Deep Stubs comes in handy.

Let’s see a simple example.

Code under test – DeepStubExample.java


package com.idodevjobs;

public class DeepStubExample {

    DeepStubDependent deepStubDependent;

    public void setDeepStubDependent(DeepStubDependent deepStubDependent) {
        this.deepStubDependent = deepStubDependent;
    }

    public DeepStubDependent getDeepStubDependent() {
        return deepStubDependent;
    }

    public String methodUnderTest(String input) {
        return deepStubDependent.getDeepStubDependentDependent().getMessage(input);
    }

    public static void main(String[] args) {
        DeepStubExample deepStubExample = new DeepStubExample();
        deepStubExample.setDeepStubDependent(new DeepStubDependent());
        deepStubExample.getDeepStubDependent().setDeepStubDependentDependent(new DeepStubDependentDependent());
        System.out.println(deepStubExample.methodUnderTest("test"));
    }
}

class DeepStubDependent {

    DeepStubDependentDependent deepStubDependentDependent;

    public void setDeepStubDependentDependent(DeepStubDependentDependent deepStubDependentDependent) {
        this.deepStubDependentDependent = deepStubDependentDependent;
    }

    public DeepStubDependentDependent getDeepStubDependentDependent() {
        return deepStubDependentDependent;
    }
}

class DeepStubDependentDependent {
    String getMessage(String text) {
        return "message " + text;
    }
}

jUnit – DeepStubExampleTest.java


package com.idodevjobs;

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

import static org.junit.Assert.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyString;

@RunWith(MockitoJUnitRunner.class)
public class DeepStubExampleTest {

    @Mock (answer = Answers.RETURNS_DEEP_STUBS)
    private DeepStubDependent deepStubDependent;

    @Test
    public void testMethodUnderTest() {
        DeepStubExample deepStubExample = new DeepStubExample();
        deepStubExample.setDeepStubDependent(deepStubDependent);
        given(deepStubDependent.getDeepStubDependentDependent().getMessage(anyString())).willReturn("some text");
        String output = deepStubExample.methodUnderTest("test");
        assertEquals("some text", output);
    }
}

In this example, the class under text DeepStubExample is dependent on DeepStubDependent which is in turn dependent on DeepStubDependentDependent.

In our test code, we are mocking DeepStubDependent with the annotation @Mock with attribute (answer = Answers.RETURNS_DEEP_STUBS), so that we get the mock of DeepStubDependent with its dependency mocked and it gives the capability to mock the method in the inner dependency DeepStubDependentDependent using

 given(deepStubDependent.getDeepStubDependentDependent().getMessage(anyString())).willReturn("some text"); 

It’s useful in situations where there are multiple levels of dependencies without the need to mock dependency at every level.

It’s also useful in working with legacy/poorly written code.

Please leave a comment if you have any questions. Please share and comment if you find this useful.

Advertisements
This entry was posted in java and tagged , , , . Bookmark the permalink.

3 Responses to Mockito Deep Stubs Example

  1. Pingback: Mockito Example – Unit testing void methods using verify and ArgumentCaptor | iDoDevJobs

  2. Pingback: Getting Started: Selenium (ChromeDriver) based UI automated test using Java, JUNIT and Maven – Example | iDoDevJobs

  3. Pingback: Powermock – How to mock static, private methods (Powermock tutorial) | iDoDevJobs

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s