Recall & Review
beginner
What is the purpose of the
@AfterEach annotation in JUnit?The
@AfterEach annotation marks a method to be run after each test method. It is used to clean up or reset resources to keep tests independent.Click to reveal answer
beginner
When is a method annotated with
@AfterEach executed?It runs immediately after each test method finishes, regardless of whether the test passed or failed.
Click to reveal answer
intermediate
Can multiple methods in the same test class have <code>@AfterEach</code> annotation?Yes, multiple
@AfterEach methods are allowed. They run in an undefined order after each test method.Click to reveal answer
intermediate
How does
@AfterEach differ from @AfterAll in JUnit?<code>@AfterEach</code> runs after every test method, while <code>@AfterAll</code> runs once after all tests in the class have run.Click to reveal answer
beginner
Give a simple example of a method using
@AfterEach.Example:<br>
@AfterEach
void cleanup() {
// reset shared resources
}Click to reveal answer
What happens if a test method throws an exception? Does the
@AfterEach method still run?✗ Incorrect
The @AfterEach method always runs after each test method, regardless of test success or failure.
Which annotation runs a method once after all tests in a class have finished?
✗ Incorrect
@AfterAll runs once after all tests in the class complete.
Can you have more than one
@AfterEach method in a test class?✗ Incorrect
JUnit allows multiple @AfterEach methods; their execution order is not guaranteed.
What is a common use case for
@AfterEach methods?✗ Incorrect
@AfterEach is typically used to clean up or reset shared resources after each test.
If you want to release a database connection after each test, which annotation should you use on the cleanup method?
✗ Incorrect
Use @AfterEach to release resources like database connections after each test.
Explain the role and timing of the
@AfterEach method in JUnit testing.Think about what happens after every test finishes.
You got /3 concepts.
Describe the difference between
@AfterEach and @AfterAll annotations.Consider how often each runs during the test lifecycle.
You got /3 concepts.