Mockito when() Method Example

1. Introduction

In Mockito, the when() method is instrumental in stubbing the behavior of methods. It allows you to specify return values or exceptions for methods of mock objects during testing. By defining how the mock object should behave in specific scenarios, when() ensures that the unit tests remain isolated from external factors, making them consistent and repeatable.

2. Example Steps

1. Create a concrete class with a method.

2. Design an interface for the concrete class.

3. Create a test class for the interface.

4. Use Mockito to mock the interface.

5. Leverage Mockito’s when() method to stub the behavior of the method.

6. Create a test method to invoke the mock object and assert the expected outcome.

7. Execute the test.

3. Mockito when() Method Example

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

// Step 2: Define an interface
interface BookStore {
    String getBookTitle(int bookId);

// Step 1: Create a concrete class implementing the interface
class MyBookStore implements BookStore {
    public String getBookTitle(int bookId) {
        // Actual implementation would fetch the book title based on bookId.
        return "Sample Book Title";

public class BookStoreTest {

    public void testGetBookTitle() {
        // Step 4: Mock the BookStore interface
        BookStore mockedBookStore = mock(BookStore.class);

        // Step 5: Stubbing the behavior of getBookTitle method
        when(mockedBookStore.getBookTitle(1001)).thenReturn("Mocked Book Title");

        // Step 6: Testing using the mock object
        String title = mockedBookStore.getBookTitle(1001);
        assertEquals("Mocked Book Title", title);


The test will execute successfully, confirming that the getBookTitle method of the mocked object returns the stubbed value "Mocked Book Title".

4. Step By Step Explanation

1. First, we outline an interface BookStore which declares a single method: getBookTitle.

2. Subsequently, we generate a concrete class, MyBookStore, that implements the BookStore interface. For the sake of this demonstration, the getBookTitle method in this class simply returns a static string.

3. Our primary testing transpires in the BookStoreTest class.

4. Within the testGetBookTitle method, we first construct a mock instance of the BookStore interface utilizing Mockito’s mock method.

5. We then use Mockito's when() method to stipulate the behavior for the getBookTitle method on the mock instance. This method, when called with the argument 1001, is instructed to return the string "Mocked Book Title".

6. Upon invoking the getBookTitle method on our mock object with the argument 1001, the stubbed response is correctly returned.

7. This example illuminates the utility of the when() method in Mockito. By enabling us to define specific behavior for methods on mock objects, it equips us to construct targeted and reliable unit tests.