0
0
JUnittesting~20 mins

assertDoesNotThrow in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit assertDoesNotThrow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
        });
    }
}
ATest passes because no exception is thrown inside the lambda.
BTest fails due to a compilation error in the lambda expression.
CTest fails because an exception is thrown inside the lambda.
DTest passes but with a warning about unused variables.
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects the lambda to run without exceptions.
assertion
intermediate
2: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
}
AassertDoesNotThrow(riskyMethod());
BassertDoesNotThrow(() -> riskyMethod());
CassertDoesNotThrow(() -> { riskyMethod(); return null; });
DassertDoesNotThrow(() -> riskyMethod() throws IOException);
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects a lambda expression or executable.
🔧 Debug
advanced
2:00remaining
Why does this assertDoesNotThrow test fail?
Examine the following test code and identify why the test fails.
JUnit
assertDoesNotThrow(() -> {
    throw new Exception("Error");
});
AThe syntax of the lambda is incorrect; it needs a return statement.
BassertDoesNotThrow cannot accept lambdas that throw any exceptions.
CThe lambda throws a checked Exception, causing the test to fail.
DThe Exception class is not imported, causing a compile error.
Attempts:
2 left
💡 Hint
assertDoesNotThrow fails if the lambda throws an exception.
🧠 Conceptual
advanced
2:00remaining
What is the main purpose of assertDoesNotThrow in JUnit tests?
Choose the best description of what assertDoesNotThrow verifies in a test.
AIt verifies that the tested code runs without throwing any exceptions.
BIt verifies that the tested code throws a specific exception.
CIt verifies that the tested code returns a non-null value.
DIt verifies that the tested code completes within a time limit.
Attempts:
2 left
💡 Hint
Think about what 'does not throw' means.
framework
expert
2: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";
}
AassertDoesNotThrow(() -> { return getData(); });
BString result = assertDoesNotThrow(getData());
CString result = assertDoesNotThrow(() -> { getData(); });
DString result = assertDoesNotThrow(() -> getData());
Attempts:
2 left
💡 Hint
assertDoesNotThrow returns the value from the lambda if no exception occurs.