Mockito Cheat Sheet

Last modified date

Comments: 0

Difference between doThrow() and thenThrow()

doThrow() is used for void return types:

Mockito.doThrow(new NullPointerException("Error adding user")).when(userService).addUser(ArgumentMatchers.any());

thenThrow() is used for non void return types:

Mockito.when(userService).addUser(ArgumentMatchers.any()).thenThrow(new NullPointerException("Error adding user"));


Leave a Reply

Your email address will not be published. Required fields are marked *

Post comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.