Challenge - 5 Problems
Void Method Mocking Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of mocking a void method with doNothing()
What will be the output of the following JUnit test using Mockito when the void method is mocked with doNothing()?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Service { void performAction() { System.out.println("Action performed"); } } public class ServiceTest { @Test void testPerformAction() { Service mockService = mock(Service.class); doNothing().when(mockService).performAction(); mockService.performAction(); System.out.println("Test completed"); } }
Attempts:
2 left
💡 Hint
Think about what doNothing() does when mocking void methods.
✗ Incorrect
The doNothing() stub tells Mockito to do nothing when the void method is called, so the original method body is not executed. Therefore, "Action performed" is not printed, only "Test completed" is printed.
❓ assertion
intermediate2:00remaining
Correct assertion to verify a void method call
Which assertion correctly verifies that a void method performAction() was called exactly once on a mock object in JUnit with Mockito?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Service { void performAction() {} } public class ServiceTest { @Test void testVerifyCall() { Service mockService = mock(Service.class); mockService.performAction(); // Which assertion is correct here? } }
Attempts:
2 left
💡 Hint
Mockito uses verify() to check method calls on mocks.
✗ Incorrect
The verify() method with times(1) checks that performAction() was called exactly once. The other options are invalid because performAction() returns void and cannot be used in assertions like assertEquals or assertTrue, and verify() does not take arguments like performAction(1).
🔧 Debug
advanced2:00remaining
Debugging a test with doThrow on a void method
Given the following test, what error will be thrown when mockService.performAction() is called?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Service { void performAction() {} } public class ServiceTest { @Test void testException() { Service mockService = mock(Service.class); doThrow(new RuntimeException("Error occurred")).when(mockService).performAction(); mockService.performAction(); } }
Attempts:
2 left
💡 Hint
doThrow() makes the mocked void method throw an exception when called.
✗ Incorrect
The doThrow() stub causes the mocked void method to throw the specified RuntimeException when called. So the test will throw RuntimeException with the message "Error occurred".
🧠 Conceptual
advanced2:00remaining
Why use doAnswer() with void methods in Mockito?
Why would a tester use doAnswer() when mocking a void method instead of doNothing() or doThrow()?
Attempts:
2 left
💡 Hint
doAnswer() lets you define what happens when the method is called.
✗ Incorrect
doAnswer() allows the tester to run custom code or logic when the void method is called, such as modifying arguments or triggering side effects. doNothing() just skips the method, and doThrow() throws an exception. doAnswer() is useful for complex behaviors.
❓ framework
expert3:00remaining
Correct Mockito syntax to mock a void method throwing checked exception
Which option correctly mocks a void method performAction() to throw a checked exception IOException when called, using Mockito in JUnit?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import java.io.IOException; class Service { void performAction() throws IOException {} } public class ServiceTest { @Test void testCheckedException() throws IOException { Service mockService = mock(Service.class); // Which option correctly mocks performAction() to throw IOException? } }
Attempts:
2 left
💡 Hint
Use doThrow() for void methods throwing checked exceptions.
✗ Incorrect
For void methods that throw checked exceptions, Mockito requires doThrow().when(mock).method() syntax. Option A causes a compile error because performAction() returns void and cannot be used in when(). Option A is invalid syntax. Option A tries to return an exception object which is invalid for void methods.