Mockito thenThrow() Method Example

1. Introduction

Mockito thenThrow() method allows testers to simulate exceptions being thrown by mocked methods. This is particularly helpful when testing error-handling paths in the code. By simulating exceptions, developers can ascertain that their code is resilient and handles failure scenarios as expected.

2. Example Steps

1. Define an interface with a method that can potentially throw an exception.

2. Implement this interface in a concrete class.

3. Create a test class for the interface.

4. Mock the interface using Mockito.

5. Use Mockito’s when() method combined with thenThrow() to make the method on the mock object throw an exception.

6. Use a try-catch block in the test method to catch the exception and verify its type.

7. Run the test.

3. Mockito thenThrow() 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.assertTrue;
import static;

// Step 1: Define an interface
interface DataService {
    int fetchData() throws DataFetchException;

class DataFetchException extends Exception {
    public DataFetchException(String message) {

// Step 2: Implement the interface in a concrete class
class SimpleDataService implements DataService {
    public int fetchData() throws DataFetchException {
        // For demonstration, this method does not throw any exception.
        return 42;

public class DataServiceTest {

    public void testDataFetchException() {
        // Step 4: Mock the DataService interface
        DataService mockedDataService = mock(DataService.class);

        // Step 5: Define exception behavior using when() and thenThrow()
        when(mockedDataService.fetchData()).thenThrow(new DataFetchException("Fetching data failed!"));

        // Step 6: Test using the mock object
        try {
            fail("Exception was expected but not thrown.");
        } catch (DataFetchException e) {
            assertTrue(e.getMessage().contains("Fetching data failed!"));


The test will run successfully, ensuring that the fetchData method of the mocked object throws the expected DataFetchException with the message "Fetching data failed!".

4. Step By Step Explanation

1. The DataService interface has been created with a method named fetchData that may throw a DataFetchException.

2. A custom exception DataFetchException is defined to represent potential data fetching errors.

3. A simple implementation of the interface, SimpleDataService, is provided. Here, the method does not actually throw any exception.

4. In the DataServiceTest class, the real testing action begins. The DataService interface is mocked.

5. Mockito’s when() method combined with thenThrow() is used to dictate that, when fetchData is called on the mock object, it should throw a DataFetchException with a specific message.

6. Within the test, an attempt to invoke fetchData is made. This action is wrapped in a try-catch block to handle and verify the exception that is thrown by the mock.

7. This demonstration highlights the utility of the thenThrow() method. It allows developers to mimic exception scenarios and validate that their code handles these exceptions properly.