Challenge - 5 Problems
AssertThrows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of this JUnit test using assertThrows?
Consider the following JUnit 5 test method. What will be the result of running this test?
JUnit
import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; class Calculator { int divide(int a, int b) { return a / b; } } public class CalculatorTest { @Test void testDivideByZero() { Calculator calc = new Calculator(); assertThrows(ArithmeticException.class, () -> calc.divide(10, 0)); } }
Attempts:
2 left
💡 Hint
Remember assertThrows expects the exception class and a lambda that triggers the exception.
✗ Incorrect
The divide method throws ArithmeticException when dividing by zero. The assertThrows checks that this exception is thrown, so the test passes.
❓ assertion
intermediate2:00remaining
Which assertThrows usage correctly verifies NullPointerException?
You want to test that calling method process(null) throws NullPointerException. Which option uses assertThrows correctly?
JUnit
class Processor { void process(String input) { if (input == null) throw new NullPointerException("Input is null"); } }
Attempts:
2 left
💡 Hint
assertThrows takes the exception class first, then a lambda expression.
✗ Incorrect
Option B correctly passes the exception class and a lambda that calls process with null. Others have wrong argument order or missing argument.
🔧 Debug
advanced2:00remaining
Why does this assertThrows test fail to catch the exception?
Examine the test below. Why does it fail even though divide(10, 0) throws ArithmeticException?
JUnit
import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; class Calculator { int divide(int a, int b) { return a / b; } } public class CalculatorTest { @Test void testDivideByZero() { Calculator calc = new Calculator(); assertThrows(ArithmeticException.class, calc.divide(10, 0)); } }
Attempts:
2 left
💡 Hint
Check the second argument to assertThrows carefully.
✗ Incorrect
assertThrows requires a lambda or executable that throws the exception when run. Passing calc.divide(10, 0) calls the method immediately, causing the exception before assertThrows runs.
🧠 Conceptual
advanced2:00remaining
What does assertThrows return and how can it be used?
In JUnit 5, assertThrows returns the caught exception. How can this feature be used in tests?
Attempts:
2 left
💡 Hint
Think about why you might want to keep the exception object after catching it.
✗ Incorrect
assertThrows returns the exception so you can check its message or other details to ensure it is the expected error.
❓ framework
expert2:00remaining
Which assertThrows usage correctly tests a checked exception in a method?
Given a method that throws IOException, which assertThrows usage correctly tests it?
JUnit
import java.io.IOException; class FileReader { void readFile(String path) throws IOException { if (path == null) throw new IOException("Path is null"); } }
Attempts:
2 left
💡 Hint
Checked exceptions must be handled or declared. Lambdas in assertThrows must handle checked exceptions properly.
✗ Incorrect
Option C uses a lambda block that can throw checked exceptions. Option C and C are similar but may cause compilation errors if the test method does not declare throws IOException. Option C is invalid if test method lacks throws declaration.