Challenge - 5 Problems
Exception Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
JUnit test behavior when no exception is thrown
Consider the following JUnit 5 test method. What will be the test result when this test runs?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class CalculatorTest { @Test void testDivide() { assertDoesNotThrow(() -> { int result = 10 / 2; }); } }
Attempts:
2 left
💡 Hint
assertDoesNotThrow passes if the code inside the lambda runs without exceptions.
✗ Incorrect
assertDoesNotThrow verifies that the code block does not throw any exceptions. Since 10 divided by 2 is valid, no exception occurs, so the test passes.
❓ assertion
intermediate2:00remaining
Correct use of assertDoesNotThrow in JUnit
Which of the following JUnit 5 assertions correctly tests that a method call does not throw any exception?
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects a lambda expression that runs code without exceptions.
✗ Incorrect
assertDoesNotThrow takes a lambda and passes if no exception is thrown during execution. Other options either expect exceptions or are invalid assertions.
🔧 Debug
advanced2:30remaining
Identify why a JUnit test with assertDoesNotThrow fails
Given the following test, why does it fail even though the method seems safe?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class ServiceTest { @Test void testProcess() { assertDoesNotThrow(() -> { process(null); }); } void process(String input) { System.out.println(input.length()); } }
Attempts:
2 left
💡 Hint
Calling length() on a null String causes an exception.
✗ Incorrect
process(null) calls input.length() where input is null, causing a NullPointerException, so assertDoesNotThrow fails.
🧠 Conceptual
advanced1:30remaining
Purpose of assertDoesNotThrow in test suites
Why would a tester use assertDoesNotThrow in a test case?
Attempts:
2 left
💡 Hint
assertDoesNotThrow is about confirming absence of exceptions.
✗ Incorrect
assertDoesNotThrow confirms that the tested code executes without exceptions, ensuring stability for that code path.
❓ framework
expert3:00remaining
Behavior of assertDoesNotThrow with checked exceptions
Consider a method that declares throwing a checked exception. How does assertDoesNotThrow handle this in JUnit 5?
JUnit
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; public class FileTest { void readFile() throws java.io.IOException { // Simulate file reading } @Test void testReadFile() { assertDoesNotThrow(() -> readFile()); } }
Attempts:
2 left
💡 Hint
Lambda passed to assertDoesNotThrow can throw checked exceptions if declared.
✗ Incorrect
assertDoesNotThrow accepts lambdas that can throw checked exceptions; it handles them by catching and failing the test if thrown.