Mockito 3 Mock

Mocking is the base of the Mockito framework. It mocks the object of the given interface or class and returned the mock object. It makes an exact replica of passed object and makes it behave exactly like the original object except for actually it doesn't.

Mockito framework provides 2 ways to mock an object.

  • static org.mockito.Mockito.mock method
  • @Mock annotation

Mockito uses ByteBuddy to create a subclass of the Mockito test class and its instance is created using Objenesis.

Mockito with Junit

Mockito is an extension of traditional test cases. it provides methods to mock, stub, and verify the instances, but it cannot do it on its own, it needs to register itself first on an entry point or hook so that whenever test class is executed mockito will be called as well.

Junit provides an entry point or @RegisterExtension ie @ExtendWith annotation, which provides functionality to extend or register 3rd party extensions with Junit test cases.

@ExtendWith(MockitoExtension.class)

import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class MockitoTestClass {}

static mock method

static org.mockito.Mockito.mock method helps us to create a mock object.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Test;

import static org.mockito.Mockito.mock;

public class AppleServiceTest {
    @Test
    void saveAppleWithoutMockTest() {
        AppleService appleService = new AppleService();
        appleService.saveApple("Macintosh");
    }

    @Test
    void saveAppleWithMockTest() {
        AppleService appleService = mock(AppleService.class);
        appleService.saveApple("Macintosh");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

@Mock annotation

@Mock annotation also creates a mock object just like the static mock method but it is a cleaner way of doing it. It's a shorthand notation of mock creation. The same mock object can be used in multiple test cases hence it minimizes repetitive mock creation code which makes the test class more readable.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        System.out.println("i love " + apple + " apple");
        return apple;
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void saveAppleWithoutMockTest() {
        AppleService appleService = new AppleService();
        appleService.saveApple("Macintosh");
    }

    @Test
    void saveAppleWithMockTest() {
        appleService.saveApple("Macintosh");
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

@Mock annotation

@Mock annotation is a clean way of creating mock instances, the same instance can be used in multiple test methods, but what if we have multiple test methods which require unique mock instance only, in simple words we dont required an instance more then once, then creating all at one place will be redundant, mockito provides an even cleaner way to inject mock instance directly into test method as a parameter.

package com.example.mokito3.sujan;

public class AppleService {
    public String saveApple(String apple) {
        String appleString = "i love " + apple + " apple";
        return appleString;
    }
}
package com.example.mokito3.sujan;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;

@ExtendWith(MockitoExtension.class)
public class AppleServiceTest {
    @Mock
    private AppleService appleService;

    @Test
    void saveAppleMockTest() {
        when(appleService.saveApple("Macintosh")).thenReturn("apple name is Macintosh");
        String appleName = appleService.saveApple("Macintosh");
        assertEquals("apple name is Macintosh", appleName);
    }

    @Test
    void saveAppleMockTest(@Mock AppleService appleService) {
        when(appleService.saveApple("Fuji")).thenReturn("apple name is Fuji");
        String appleName = appleService.saveApple("Fuji");
        assertEquals("apple name is Fuji", appleName);
    }
}
plugins {
    id 'java'
}

group 'org.example'
version '1.0-SNAPSHOT'

repositories { jcenter() }
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
    testCompile 'org.mockito:mockito-junit-jupiter:3.4.4'
}
test {
    useJUnitPlatform()
}

follow us on