What if you could test actions that don't give answers back, without messy manual checks?
Why Mocking void methods in JUnit? - Purpose & Use Cases
Imagine you have a method that just sends an email or logs a message but doesn't return anything. You want to test your code that calls this method, but since it doesn't give back a result, how do you check if it worked correctly?
Manually testing such methods means you have to check external effects like email inboxes or log files. This is slow, unreliable, and hard to automate. You might miss errors or waste time repeating the same checks.
Mocking void methods lets you replace these methods with fake versions that do nothing or track calls. This way, you can test your code's behavior without real side effects, making tests fast, reliable, and easy to write.
emailService.sendEmail(user, message);
// Manually check inbox or logsdoNothing().when(emailService).sendEmail(any(), any()); verify(emailService).sendEmail(user, message);
It enables automated tests to confirm that important actions happened without causing real side effects.
Testing that a notification is sent when a user signs up, without actually sending emails every time you run tests.
Void methods don't return values, making them tricky to test.
Manual checks are slow and error-prone.
Mocking void methods lets tests run fast and reliably by simulating behavior.
Practice
Solution
Step 1: Understand void methods in tests
Void methods do not return values, so mocking helps control their side effects during tests.Step 2: Purpose of mocking void methods
Mocking allows skipping actual execution and verifying if the method was called, without running real code.Final Answer:
To control their behavior and verify interactions without executing actual code -> Option BQuick Check:
Mock void methods = control behavior + verify calls [OK]
- Thinking void methods return values
- Assuming mocking changes method return type
- Believing mocking disables all tests
sendEmail() to do nothing when called?Solution
Step 1: Recall Mockito syntax for void methods
Mockito usesdoNothing().when(mock).method()to mock void methods doing nothing.Step 2: Analyze options
doNothing().when(mock).sendEmail(); matches correct syntax; others misusewhen()orverify().Final Answer:
doNothing().when(mock).sendEmail(); -> Option AQuick Check:
Mock void method to do nothing = doNothing().when() [OK]
- Using when() with void methods incorrectly
- Trying to chain doNothing() after method call
- Confusing verify() with mocking syntax
doThrow(new RuntimeException()).when(mock).deleteFile(); mock.deleteFile();
What happens when
mock.deleteFile() is called?Solution
Step 1: Understand doThrow() usage
doThrow() tells Mockito to throw the specified exception when the void method is called.Step 2: Analyze method call effect
Callingmock.deleteFile()triggers the RuntimeException as mocked.Final Answer:
A RuntimeException is thrown -> Option DQuick Check:
doThrow() causes exception on void method call [OK]
- Expecting method to run normally
- Thinking void methods return null
- Confusing doThrow() with doNothing()
doNothing().when(mock.deleteFile());
Solution
Step 1: Check Mockito syntax for mocking void methods
Correct syntax isdoNothing().when(mock).method(), not passing method call inside when().Step 2: Identify error in code
Code incorrectly calls method inside when(), causing syntax error.Final Answer:
Incorrect use of when() with method call inside -> Option AQuick Check:
when() expects mock object, not method call [OK]
- Calling method inside when() instead of after when(mock)
- Thinking doNothing() needs exception type
- Believing doNothing() can't mock void methods
logAction() was called exactly twice during a test. Which Mockito code correctly does this?Solution
Step 1: Recall Mockito verify syntax for call count
Useverify(mock, times(n)).method()to check method called n times.Step 2: Analyze options
verify(mock, times(2)).logAction(); uses correct syntax; others misuse verify or when with void methods.Final Answer:
verify(mock, times(2)).logAction(); -> Option CQuick Check:
Verify call count = verify(mock, times(n)) [OK]
- Using when() with void methods to check calls
- Misplacing times() inside method call
- Not specifying times() for multiple calls
Ready to prove it?
Pass the test and this pattern moves unknown
