Part 2 – How To Mock Methods in Mockito

How To Mock Methods in Mockito

What have we learned so far
Part 1 – What is Mockito?

In this tutorial, we will understand below topics –
– What is a Mock Object?
– When should I mock?
– Mockito Framework
– Enable Mockito Annotations
– How to Mock Object with @Mock & @InjectMock annotations in Spring application?

Ways to mock methods in Mockito

Now we have successfully created and injected the mock, and now we should tell the mock how to behave when certain methods are called on it. We can achieve this by using the below patterns

1. when/then

when(repository.saveCusomer(any())).thenReturn(true);

Here,
when: when is a static method of the Mockito class which takes an object and its method which needs to be mocked
any(): It’s a matcher which specifies that the method may get a call with any parameter.
thenRetun: What value do you want to return when this method is called with the specified parameters.

package com.onlyfullstack.unittesting.service;

import com.onlyfullstack.unittesting.bean.Customer;
import com.onlyfullstack.unittesting.repository.CustomerRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * This class contains usage of Mockito
 */
@RunWith(MockitoJUnitRunner.class)
public class CustomerServiceTest {

    @Mock
    CustomerRepository repository;

    @InjectMocks
    CustomerService customerService;

    @Test
    public void saveCustomer_withValidCustomer() {
        Customer customer = new Customer(6, "QQQ", "Mumbai");
        when(repository.saveCusomer(any())).thenReturn(true);
        Boolean save = customerService.saveCustomer(customer);
        assertThat(true, is(save));
    }

}

We have seen how to use when/thenRetun pattern, lets explore other patterns:

2. when/thenThrow
This method will throw the specified exception when the mocked method is called.

@Test(expected = IllegalStateException.class)
public void saveCustomer_withValidCustomer_when_thenThrow() {
    Customer customer = new Customer(6, "QQQ", "Mumbai");
    when(repository.saveCusomer(any())).thenThrow(new IllegalStateException());
    customerService.saveCustomer(customer);
}

3. When/thenAnswer

we will look into this into our next tutorial
Source Code
Download the source code of JUnit tutorial from below git repository :
unit-testing-and-integration-testing-with-spring-boot

Let’s go to our next tutorial where we will discuss below points :

Part 3- How to mock void methods with Mockito
 – Mocking Void Methods
– Three ways to mock the void method:
1. doNothing/when
2. doAnswer/when
3. doThrow/when
https://www.onlyfullstack.com/how-to-mock-void-methods-with-mockito/