0
0
JUnittesting~20 mins

assertThrows usage in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AssertThrows Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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));
    }
}
ATest passes because ArithmeticException is thrown as expected.
BTest fails because no exception is thrown.
CTest fails with a compilation error due to wrong assertThrows usage.
DTest passes but throws NullPointerException instead.
Attempts:
2 left
💡 Hint
Remember assertThrows expects the exception class and a lambda that triggers the exception.
assertion
intermediate
2: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");
    }
}
AassertThrows(() -> processor.process(null), NullPointerException.class);
BassertThrows(NullPointerException.class, () -> processor.process(null));
CassertThrows(NullPointerException.class, processor.process(null));
DassertThrows(NullPointerException.class, () -> processor.process());
Attempts:
2 left
💡 Hint
assertThrows takes the exception class first, then a lambda expression.
🔧 Debug
advanced
2: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));
    }
}
AThe test fails because assertThrows requires a try-catch block inside.
BThe test fails because ArithmeticException is not thrown by divide method.
CThe test fails because the exception class is incorrect.
DThe test fails because assertThrows expects a lambda, but a method call is passed instead.
Attempts:
2 left
💡 Hint
Check the second argument to assertThrows carefully.
🧠 Conceptual
advanced
2: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?
ATo verify exception message or properties by inspecting the returned exception object.
BTo suppress the exception so the test continues without failure.
CTo automatically retry the test if the exception is thrown.
DTo convert checked exceptions into unchecked exceptions.
Attempts:
2 left
💡 Hint
Think about why you might want to keep the exception object after catching it.
framework
expert
2: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");
    }
}
AassertThrows(IOException.class, () -> fileReader.readFile(null));
BassertThrows(IOException.class, () -> fileReader.readFile(null)); // with throws declaration on test method
CassertThrows(IOException.class, () -> { fileReader.readFile(null); });
DassertThrows(IOException.class, () -> fileReader.readFile(null)); // without throws declaration on test method
Attempts:
2 left
💡 Hint
Checked exceptions must be handled or declared. Lambdas in assertThrows must handle checked exceptions properly.