Stubbing Void Methods
Fast recall
Quick revision
Core answer
do...when family because when(...).thenReturn(...) needs a value expression. An unstubbed void method on a normal mock already does nothing. Use doThrow to simulate failure, doAnswer for controlled behavior, and doNothing mainly to suppress a real spy method or make a consecutive sequence explicit.Interview-ready guide
Interview answer
Void methods use the do...when style because when(...).thenReturn(...) needs a returned value. On a normal Mockito mock, an unstubbed void method already does nothing, so I do not add doNothing() for every success path. I use doThrow(new MailException(...)).when(sender).send(message) to test error handling, and doAnswer(...) when the void call must invoke a callback or record controlled test state. doNothing() is useful when a spy's real void method must be suppressed, or when defining consecutive behavior such as succeed once and fail next. Stubbing controls what happens; verify(sender).send(message) is the separate assertion that the interaction occurred.
Stubbing Void Methods
Direct Answer
Void methods use Mockito's do...when family because when(...).thenReturn(...) needs a value expression. An unstubbed void method on a normal mock already does nothing. Use doThrow to simulate failure, doAnswer for controlled behavior, and doNothing mainly to suppress a real spy method or make a consecutive sequence explicit.
Code Example
@ExtendWith(MockitoExtension.class)
class NotificationServiceTest {
@Mock
private EmailSender emailSender;
@InjectMocks
private NotificationService notificationService;
@Test
void testSuccessfulNotification() {
// Stub void method to do nothing
doNothing().when(emailSender).sendEmail(anyString(), anyString());
notificationService.notifyUser("user@example.com", "Welcome!");
verify(emailSender).sendEmail("user@example.com", "Welcome!");
}
@Test
void testNotificationFailure() {
// Stub void method to throw exception
doThrow(new EmailServiceException("SMTP failure"))
.when(emailSender).sendEmail(anyString(), anyString());
assertThrows(EmailServiceException.class, () ->
notificationService.notifyUser("user@example.com", "Alert"));
}
}
Common Mistakes & Pitfalls
- Using when().thenReturn() on void methods: Freshers often try when(mock.voidMethod()).thenReturn() which won't compile because void has no return type. The compiler error "void type not allowed here" confuses beginners who don't realize void methods need the do-family API.
- Forgetting doNothing() is the default: Many write doNothing() explicitly when it's unnecessary — mocked void methods do nothing by default. You only need doNothing() for readability or when chaining multiple stubbings.
- Not using doThrow() for exception testing: Freshers sometimes try to make void methods throw exceptions using when-syntax, missing that doThrow() is the correct approach for testing error handling in void method callers.
Check your understanding
Answer these without looking back at the article.
- When would doAnswer() be preferred over doNothing()?
- Why must you use doReturn() instead of thenReturn() when stubbing a Spy?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.