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 result of this JUnit test using assertThrows?
Consider the following JUnit 5 test method. What will be the test execution result?
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
Think about what happens when dividing by zero in Java.
✗ Incorrect
Dividing an integer by zero in Java throws an ArithmeticException. The assertThrows method expects this exception, 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 assertThrows statement is correct?
JUnit
class Processor { void process(String input) { if (input == null) throw new NullPointerException(); } }
Attempts:
2 left
💡 Hint
The exception type must match exactly the one thrown.
✗ Incorrect
Only option D expects NullPointerException and calls process with null, which triggers the exception.
🔧 Debug
advanced2:00remaining
Why does this assertThrows test fail unexpectedly?
Examine the test below. It is supposed to check that getItem(-1) throws IndexOutOfBoundsException, but the test fails. Why?
JUnit
import static org.junit.jupiter.api.Assertions.assertThrows; import org.junit.jupiter.api.Test; import java.util.List; public class ListTest { @Test void testGetItem() { List<String> list = List.of("a", "b", "c"); assertThrows(IndexOutOfBoundsException.class, () -> list.get(1)); } }
Attempts:
2 left
💡 Hint
Check the index used in list.get and the list size.
✗ Incorrect
Index 1 is valid for the list of size 3, so no exception is thrown and assertThrows fails.
🧠 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 what you can do with the exception object after catching it.
✗ Incorrect
assertThrows returns the exception object, allowing assertions on its message or fields.
❓ framework
expert2:00remaining
How to test a method that throws a checked exception using assertThrows?
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 require handling or declaration in lambda.
✗ Incorrect
Option A uses a block lambda that allows throwing checked IOException, matching the method signature.