Home Arrow Icon Knowledge base Arrow Icon Global Arrow Icon What are the differences between doThrow() and thenThrow() in Mockito


What are the differences between doThrow() and thenThrow() in Mockito


In Mockito, `doThrow()` and `thenThrow()` are both used to stub methods to throw exceptions, but they serve slightly different purposes and are used in different contexts.

doThrow()

- Usage for Void Methods: The primary use of `doThrow()` is for methods that return `void`. Since `when()` requires a return value to chain `thenReturn()` or `thenThrow()`, it cannot be used directly with void methods. Therefore, `doThrow()` is essential for stubbing void methods to throw exceptions[3][7][9].

- Safety and Avoiding Side Effects: `doThrow()` is considered safer because it does not invoke the method on the mock object when setting up the stub. This is important when the method being stubbed has side effects or when using spies (`@Spy`), as it prevents unintended behavior during test setup[1][3].

- Example:

java
  doThrow(new IOException()).when(mockStream).close();
  

thenThrow()

- Usage for Non-Void Methods: `thenThrow()` is typically used with non-void methods. It is part of the `when()` method chain, which makes it more readable and fluent in English-like syntax[1][3][9].

- Readability: The `when().thenThrow()` syntax is often preferred for its readability, as it follows a more natural sentence structure. However, it requires the method to be called during the stubbing process, which can lead to issues if the method has side effects or is not properly stubbed beforehand[1][3].

- Example:

java
  when(mockClass.someMethod()).thenThrow(new RuntimeException("An error occurred"));
  

Key Differences

- Method Type: `doThrow()` is necessary for void methods, while `thenThrow()` is used with non-void methods.
- Safety and Side Effects: `doThrow()` avoids invoking the method during stubbing, making it safer for methods with side effects.
- Readability: `when().thenThrow()` is generally more readable due to its fluent syntax.

In summary, while both methods can be used to throw exceptions, the choice between them depends on the method type (void or non-void) and the need to avoid side effects during stubbing.

Citations:
[1] https://stackoverflow.com/questions/36343149/mockito-difference-between-dothrow-and-thenthrow
[2] https://www.lambdatest.com/automation-testing-advisor/selenium/methods/org.mockito.Mockito.doThrow
[3] https://www.vogella.com/tutorials/Mockito/article.html
[4] https://site.mockito.org/javadoc/current/org/mockito/Mockito.html
[5] https://www.javaguides.net/2023/10/mockito-dothrow.html
[6] https://stackoverflow.com/questions/15156857/mockito-test-a-void-method-throws-an-exception/52991225
[7] https://community.lambdatest.com/t/how-can-i-use-mockito-to-mock-and-assert-a-thrown-exception-in-a-junit-test/31705
[8] https://www.digitalocean.com/community/tutorials/mockito-mock-void-method
[9] https://www.browserstack.com/guide/mockito-throw-exception