0
0
JUnittesting~20 mins

when().thenThrow() for exceptions in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mockito Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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();
    }
}
ATest fails with IOException: IO error
BTest passes without exception
CTest fails with NullPointerException
DTest fails with RuntimeException
Attempts:
2 left
💡 Hint
when().thenThrow() makes the mock throw the specified exception when the method is called.
assertion
intermediate
2: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?
    }
}
AassertDoesNotThrow(() -> mockCalc.divide(10, 0));
BassertTrue(mockCalc.divide(10, 0) == 0);
CassertThrows(ArithmeticException.class, () -> mockCalc.divide(10, 0));
DassertEquals(0, mockCalc.divide(10, 0));
Attempts:
2 left
💡 Hint
Use assertThrows to check if a method call throws an exception.
🔧 Debug
advanced
2: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();
    }
}
AMocking void method with when() is correct
BthenThrow() cannot throw Exception objects
CNo error, test will pass
DCannot throw checked Exception without declaring it or handling it
Attempts:
2 left
💡 Hint
Check if the exception thrown is checked and if the method signature allows it.
🧠 Conceptual
advanced
2:00remaining
Difference between thenThrow() and doThrow() in Mockito
Which statement correctly describes the difference between thenThrow() and doThrow() when mocking exceptions?
AthenThrow() throws checked exceptions; doThrow() throws unchecked exceptions only
BthenThrow() is used for methods with return values; doThrow() is used for void methods
CthenThrow() can only throw RuntimeException; doThrow() can throw any Throwable
DthenThrow() is deprecated; doThrow() is the recommended replacement
Attempts:
2 left
💡 Hint
Consider method return types when choosing between thenThrow() and doThrow().
framework
expert
2: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());
        }
    }
}
AIllegalArgumentException with message 'Second call failed'
BIllegalStateException with message 'First call failed'
CNo exception thrown on second call
DRuntimeException with no message
Attempts:
2 left
💡 Hint
Mockito chains thenThrow calls to throw exceptions in order on consecutive calls.