0
0
JUnittesting~5 mins

Testing no exception thrown in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AThe test retries automatically
BThe test fails
CThe test is ignored
DThe test passes
Which JUnit 5 method explicitly checks that no exception is thrown?
AassertThrows
BassertEquals
CassertDoesNotThrow
DassertNull
If you want to test that a method runs without exceptions, what is the simplest approach?
AJust call the method in the test without extra code
BWrap the call in try-catch and fail on catch
CUse assertThrows with no exception
DUse fail() method
Why might you prefer assertDoesNotThrow over just calling the method in a test?
AIt ignores exceptions
BIt catches exceptions silently
CIt slows down the test
DIt makes the test intention clearer
What tag would you use to mark a test that verifies no exceptions are thrown?
A@Test
B@Test(timeout=1000)
C@Test(expected=Exception.class)
D@Ignore
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.