0
0
JUnittesting~20 mins

Testing no exception thrown in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
        });
    }
}
AThe test fails with a compilation error due to missing exception handling.
BThe test fails because assertDoesNotThrow expects an exception to be thrown.
CThe test passes because no exception is thrown during division.
DThe test is ignored because no assertion is made.
Attempts:
2 left
💡 Hint
assertDoesNotThrow passes if the code inside the lambda runs without exceptions.
assertion
intermediate
2: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?
AassertDoesNotThrow(() -> myObject.myMethod());
BassertTrue(() -> myObject.myMethod());
CassertThrows(Exception.class, () -> myObject.myMethod());
DassertEquals(null, myObject.myMethod());
Attempts:
2 left
💡 Hint
assertDoesNotThrow expects a lambda expression that runs code without exceptions.
🔧 Debug
advanced
2: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());
    }
}
AThe test fails because assertDoesNotThrow only works with checked exceptions.
BassertDoesNotThrow requires a try-catch block inside the lambda.
CThe test fails because System.out.println cannot print null values.
DThe method process throws a NullPointerException when input is null.
Attempts:
2 left
💡 Hint
Calling length() on a null String causes an exception.
🧠 Conceptual
advanced
1:30remaining
Purpose of assertDoesNotThrow in test suites
Why would a tester use assertDoesNotThrow in a test case?
ATo verify that a specific block of code runs without throwing any exceptions.
BTo assert that a method returns a non-null value.
CTo check that an exception is thrown by the code under test.
DTo skip the test if an exception occurs.
Attempts:
2 left
💡 Hint
assertDoesNotThrow is about confirming absence of exceptions.
framework
expert
3: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());
    }
}
AassertDoesNotThrow only works with unchecked exceptions and will fail at compile time with checked exceptions.
BassertDoesNotThrow can handle checked exceptions if the lambda declares them or wraps them properly.
CassertDoesNotThrow automatically catches and ignores all exceptions, checked or unchecked.
DassertDoesNotThrow requires the test method to declare the checked exception in its throws clause.
Attempts:
2 left
💡 Hint
Lambda passed to assertDoesNotThrow can throw checked exceptions if declared.