How to Use doThrow in Mockito with JUnit Tests
Use
doThrow() in Mockito to simulate exceptions from void methods in JUnit tests. It lets you specify an exception to throw when a mocked void method is called, using syntax like doThrow(new Exception()).when(mock).voidMethod().Syntax
The doThrow() method is used to specify an exception that a mocked void method should throw when called. It is followed by .when(mockObject).methodCall() to define which method triggers the exception.
- doThrow(exception): Defines the exception to throw.
- when(mockObject): Specifies the mock object.
- methodCall(): The void method call that triggers the exception.
java
doThrow(new RuntimeException("Error message")).when(mockObject).voidMethod();
Example
This example shows how to use doThrow() to simulate an exception from a void method in a JUnit test. The test verifies that the exception is thrown as expected.
java
import static org.mockito.Mockito.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import org.mockito.Mockito; class Service { void performAction() { // real implementation } } public class DoThrowExampleTest { @Test void testDoThrowOnVoidMethod() { Service mockService = Mockito.mock(Service.class); // Setup doThrow to throw RuntimeException when performAction is called doThrow(new RuntimeException("Forced exception")).when(mockService).performAction(); // Verify that calling performAction throws the exception RuntimeException thrown = assertThrows(RuntimeException.class, () -> { mockService.performAction(); }); assertEquals("Forced exception", thrown.getMessage()); } }
Output
Test passed: RuntimeException with message 'Forced exception' was thrown as expected.
Common Pitfalls
Common mistakes when using doThrow() include:
- Using
when(mock.method()).thenThrow()with void methods, which causes compilation errors because void methods return nothing. - Not mocking the correct method or mock object, so the exception is never thrown.
- Forgetting to import static Mockito methods, leading to verbose code.
Always use doThrow() for void methods instead of thenThrow().
java
/* Wrong way: causes compile error for void methods */ // when(mockService.performAction()).thenThrow(new RuntimeException()); /* Right way: use doThrow for void methods */ doThrow(new RuntimeException()).when(mockService).performAction();
Quick Reference
| Usage | Description |
|---|---|
| doThrow(exception).when(mock).voidMethod() | Throws exception when void method is called |
| doNothing().when(mock).voidMethod() | Does nothing (default for void methods) |
| verify(mock).voidMethod() | Verifies void method was called |
| thenThrow(exception).when(mock).method() | Throws exception for non-void methods |
Key Takeaways
Use doThrow() to simulate exceptions from void methods in Mockito with JUnit.
doThrow() syntax: doThrow(exception).when(mock).voidMethod().
Never use thenThrow() with void methods; it causes compile errors.
Always verify your mock and method to ensure exceptions are triggered correctly.
Import static Mockito methods for cleaner test code.