Mockito Tutorial

October 15, 2022 No comments Mockito Java Tutorial
Mockito Tutorial

Mockito is an open source Java framework for testing code. Mockito allows to mock objects and proxy method executions in automated unit tests. Mockito is helpful in creating mocks and spies in a simple and intuitive way, while at the same time providing great control of the whole process.

Getting Started with Mockito

Mockito provides several annotations to reduce the line of code to make the code easy to read and maintain.

  • @Mock - for creating mock objects, we can specify how a it should behave,
  • @Spy - is for partial mocking, real methods are invoked, but we can still choose which one will be stubbed,
  • @InjectMocks - inject objects annotated with @Spy or @Mock into test class,
  • @Captor - mark special objects used to capturing the arguments passed into methods.

Useful Mockito articles

Spring Boot 2 + JUnit 5 + Mockito

Mocking calls to a method that returns void using Mockito

How to verify the order of calls using Mockito

Difference between Mockito.mock() method and the @Mock annotation

How to make a mocked method return an argument that was passed to it using Mockito

Compare doAnswer and thenReturn methods in Mockito

How to check if method was never called using Mockito

How to capture arguments of a method called multiple times using Mockito

When we should use @Mock and @InjectMocks annotations

How to mock static methods with Mockito

Verify a method is called two times with Mockito

How to stub a method to return different objects on subsequent invocations using Mockito

Mocking final method with Mockito framework

Throwing an exception when calling void method using Mockito

Why trying to spy on method is calling the original method in Mockito

Mock multiple calls with Mockito

Mock classes with generic parameters using Mockito

Using ArgumentCaptor to capture a list of specific type with Mockito

What to do if test a void method throws an exception in Mockito


Mockito Cheat Sheet

Enable Mockito Annotations

To start using Mockito annotations we need to enable them first. There are three options to initialize them:

  1. Mark JUnit test class with a @RunWith(MockitoJUnitRunner.class)

    Using JUnit 4:
    import org.mockito.junit.MockitoJUnitRunner;
    import org.junit.runner.RunWith;
    
    @RunWith(MockitoJUnitRunner.class)
    public class MockitoTest {
    }
    Using JUnit 5:
    import org.mockito.junit.jupiter.MockitoExtension;
    import org.junit.jupiter.api.extension.ExtendWith;
    
    @ExtendWith(MockitoExtension.class)
    public class MockitoTest {
    }
  2. Call MockitoAnnotations.initMocks() in test class set up method usually annotated with @Before
    public class MockitoTest {
        @Before
        public void setUp() {
            MockitoAnnotations.initMocks()
        } 
    }
  3. Use dedicated @Rule public MockitoRule rule = MockitoJUnit.rule();
    public class MockitoTest {
        @Rule 
        public MockitoRule rule = MockitoJUnit.rule();
    }
{{ message }}

{{ 'Comments are closed.' | trans }}