Recall & Review
beginner
What does it mean to test that no exception is thrown in JUnit?
It means verifying that a piece of code runs without causing any errors or exceptions. The test passes if no exceptions occur during execution.
Click to reveal answer
beginner
How do you write a JUnit test to check that no exception is thrown?
Simply write the test method to call the code. If the code runs without throwing exceptions, the test passes. No special annotation is needed for this.
Click to reveal answer
beginner
Why is it important to test that no exception is thrown?
Because it confirms the code behaves as expected under normal conditions and does not crash or fail unexpectedly.
Click to reveal answer
beginner
What happens if an exception is thrown during a JUnit test that expects no exception?
The test fails immediately, showing the exception details in the test report.
Click to reveal answer
intermediate
Can you use assertDoesNotThrow in JUnit 5 to test no exception is thrown? How?
Yes. Use assertDoesNotThrow(() -> { code }); to explicitly check no exception is thrown. It improves test clarity.
Click to reveal answer
In JUnit, what happens if your test method throws an unexpected exception?
✗ Incorrect
If an unexpected exception is thrown, JUnit marks the test as failed.
Which JUnit 5 method explicitly checks that no exception is thrown?
✗ Incorrect
assertDoesNotThrow verifies that the code block runs without throwing exceptions.
If you want to test that a method runs without exceptions, what is the simplest approach?
✗ Incorrect
Simply calling the method is enough; if an exception occurs, the test fails automatically.
Why might you prefer assertDoesNotThrow over just calling the method in a test?
✗ Incorrect
assertDoesNotThrow clearly shows the test expects no exceptions, improving readability.
What tag would you use to mark a test that verifies no exceptions are thrown?
✗ Incorrect
Just @Test is used; no special annotation is needed to test no exceptions.
Explain how to write a JUnit test that verifies no exception is thrown when running a method.
Think about what happens if an exception is thrown during a test.
You got /4 concepts.
Describe the difference between using assertDoesNotThrow and just calling the method in a JUnit test.
Consider clarity and intention in tests.
You got /4 concepts.