Mockito doThrow()

1. Overview

The Mockito.doThrow() method is an essential tool when you want to simulate exceptions in your mock objects during testing. By causing specific methods to throw exceptions, you can validate how your software handles these scenarios, ensuring resilience and robustness. In this tutorial, we will dive into the doThrow() method and learn its practical applications.

2. Development Steps

1. Set up a new Maven project.

2. Incorporate the necessary Mockito and JUnit 5 dependencies.

3. Implement a simple class named 'Notifier' to demonstrate the use of doThrow().

4. Construct a test class to exhibit the Mockito.doThrow() usage.

5. Run the test cases and analyze the outcomes.

6. Interpret the results.

3. Dependencies (Mockito and JUnit 5)

<!-- JUnit 5 -->
<!-- Mockito -->

4. Code Program

// Step 3: Construct the Notifier class
class Notifier {
    void notify(String message) {
        System.out.println("Notification sent: " + message);
// Step 4: Create the test class for Notifier
import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.doThrow;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class NotifierTest {
    public void testDoThrow() {
        // Generate a mock of Notifier
        Notifier mockNotifier = mock(Notifier.class);
        // Use doThrow to simulate an exception when notify method is called
        doThrow(new RuntimeException("Notification Error")).when(mockNotifier).notify("Test Message");
        // Assert that an exception is thrown when calling the notify method on the mock
        assertThrows(RuntimeException.class, () -> mockNotifier.notify("Test Message"));


The testDoThrow test case will succeed, affirming that the notify("Test Message") method on the mock Notifier throws the intended exception.

Code Explanation:

1. We began with a simple Notifier class with a single method named notify that outputs a notification message.

2. In our test class, we created a mock of the Notifier using Mockito's mock() method.

3. We then employed the doThrow().when() idiom to cause the notify method to throw a RuntimeException when invoked with the message "Test Message".

4. We proceeded to verify that calling the notify method with the specified message indeed throws the intended exception. This is achieved with JUnit 5's assertThrows() method.

5. Conclusion

Using Mockito.doThrow(), testers and developers can conveniently simulate exception scenarios, facilitating rigorous testing of error handling and exception-related code paths. This method enhances your tests by adding a layer of validation for unexpected situations, making your software more reliable and robust.

Related Mockito Methods

Mockito mock()
Mockito spy()
Mockito when()
Mockito thenThrow()
Mockito verify()
Mockito times()
Mockito never()
Mockito any()
Mockito eq()
Mockito inOrder()
Mockito doReturn()
Mockito doThrow()
Mockito doAnswer()
Mockito timeout()
Mockito ArgumentMatchers