Challenge - 5 Problems
Mockito Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of when().thenThrow() with checked exception
What will be the output when the following JUnit test runs?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; import java.io.IOException; class Service { void perform() throws IOException { // does something } } public class ServiceTest { @Test void testPerformThrows() throws IOException { Service mockService = mock(Service.class); when(mockService.perform()).thenThrow(new IOException("IO error")); mockService.perform(); } }
Attempts:
2 left
💡 Hint
when().thenThrow() makes the mock throw the specified exception when the method is called.
✗ Incorrect
The mock is set to throw an IOException when perform() is called. Since the test calls perform(), it throws IOException causing the test to fail unless the exception is caught or expected.
❓ assertion
intermediate2:00remaining
Correct assertion to verify exception thrown by mock
Which assertion correctly verifies that the mocked method throws the expected exception?
JUnit
import static org.mockito.Mockito.*; import static org.mockito.ArgumentMatchers.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; class Calculator { int divide(int a, int b) { return a / b; } } public class CalculatorTest { @Test void testDivideThrows() { Calculator mockCalc = mock(Calculator.class); when(mockCalc.divide(anyInt(), eq(0))).thenThrow(new ArithmeticException("Divide by zero")); // Which assertion below is correct? } }
Attempts:
2 left
💡 Hint
Use assertThrows to check if a method call throws an exception.
✗ Incorrect
assertThrows expects the specified exception to be thrown by the lambda expression. This matches the mock setup that throws ArithmeticException when dividing by zero.
🔧 Debug
advanced2:00remaining
Identify the error in when().thenThrow() usage
What is the error in the following test code that uses when().thenThrow()?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Network { void connect() { // connects to network } } public class NetworkTest { @Test void testConnectThrows() { Network mockNetwork = mock(Network.class); when(mockNetwork.connect()).thenThrow(new Exception("Connection failed")); mockNetwork.connect(); } }
Attempts:
2 left
💡 Hint
Check if the exception thrown is checked and if the method signature allows it.
✗ Incorrect
connect() does not declare throwing checked Exception, so thenThrow(new Exception()) causes a compile error. For void methods, doThrow() should be used instead.
🧠 Conceptual
advanced2:00remaining
Difference between thenThrow() and doThrow() in Mockito
Which statement correctly describes the difference between thenThrow() and doThrow() when mocking exceptions?
Attempts:
2 left
💡 Hint
Consider method return types when choosing between thenThrow() and doThrow().
✗ Incorrect
thenThrow() is used to stub methods that return a value to throw exceptions. doThrow() is used to stub void methods to throw exceptions.
❓ framework
expert2:00remaining
JUnit test behavior with chained thenThrow() calls
Given the following mock setup, what exception is thrown on the second call to process()?
JUnit
import static org.mockito.Mockito.*; import org.junit.jupiter.api.Test; class Processor { String process() { return "done"; } } public class ProcessorTest { @Test void testProcessMultipleExceptions() { Processor mockProcessor = mock(Processor.class); when(mockProcessor.process()) .thenThrow(new IllegalStateException("First call failed")) .thenThrow(new IllegalArgumentException("Second call failed")); try { mockProcessor.process(); } catch (Exception e) { System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage()); } try { mockProcessor.process(); } catch (Exception e) { System.out.println(e.getClass().getSimpleName() + ": " + e.getMessage()); } } }
Attempts:
2 left
💡 Hint
Mockito chains thenThrow calls to throw exceptions in order on consecutive calls.
✗ Incorrect
The first call throws IllegalStateException, the second call throws IllegalArgumentException as configured by chained thenThrow calls.