Challenge - 5 Problems
JUnit assertDoesNotThrow 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 assertDoesNotThrow?
Consider the following JUnit test method. What will be the test execution result?
JUnit
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import org.junit.jupiter.api.Test; public class SampleTest { @Test void testNoException() { assertDoesNotThrow(() -> { int a = 5; int b = 10; int c = a + b; }); } }
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects the lambda to run without exceptions.
✗ Incorrect
The lambda inside assertDoesNotThrow runs code that does not throw any exceptions, so the test passes.
❓ assertion
intermediate2:00remaining
Which assertion correctly uses assertDoesNotThrow to check a method that might throw?
You have a method that might throw an IOException. Which assertion correctly tests that it does NOT throw any exception?
JUnit
void riskyMethod() throws IOException {
// might throw IOException
}Attempts:
2 left
💡 Hint
assertDoesNotThrow expects a lambda expression or executable.
✗ Incorrect
Option B correctly passes a lambda that calls riskyMethod. Option B calls the method immediately, causing a compile error. Option B returns null unnecessarily but compiles. Option B is invalid syntax.
🔧 Debug
advanced2:00remaining
Why does this assertDoesNotThrow test fail?
Examine the following test code and identify why the test fails.
JUnit
assertDoesNotThrow(() -> {
throw new Exception("Error");
});Attempts:
2 left
💡 Hint
assertDoesNotThrow fails if the lambda throws an exception.
✗ Incorrect
The lambda throws a checked Exception, causing assertDoesNotThrow to fail the test.
🧠 Conceptual
advanced2:00remaining
What is the main purpose of assertDoesNotThrow in JUnit tests?
Choose the best description of what assertDoesNotThrow verifies in a test.
Attempts:
2 left
💡 Hint
Think about what 'does not throw' means.
✗ Incorrect
assertDoesNotThrow checks that no exceptions are thrown during execution of the tested code.
❓ framework
expert2:00remaining
Which option correctly captures the return value when using assertDoesNotThrow?
You want to test a method that returns a value and ensure it does not throw an exception. How do you capture the return value using assertDoesNotThrow?
JUnit
public String getData() throws Exception {
return "data";
}Attempts:
2 left
💡 Hint
assertDoesNotThrow returns the value from the lambda if no exception occurs.
✗ Incorrect
Option D correctly assigns the returned value from assertDoesNotThrow. Option D is invalid syntax. Option D does not assign the return value. Option D returns void lambda, so cannot assign.