Recall & Review
beginner
What is the purpose of the
@AfterAll method in JUnit?The <code>@AfterAll</code> method runs once after all test methods in a class have finished. It is used to clean up resources shared by tests, like closing database connections.Click to reveal answer
intermediate
How must a method annotated with
@AfterAll be declared in JUnit 5?It must be <strong>static</strong> unless the test class is annotated with <code>@TestInstance(TestInstance.Lifecycle.PER_CLASS)</code>. This ensures it runs once after all tests.Click to reveal answer
beginner
Example of a simple
@AfterAll method in JUnit 5: @AfterAll
static void cleanup() {
System.out.println("All tests done. Cleaning up.");
}Click to reveal answer
intermediate
What happens if an exception is thrown inside an
@AfterAll method?The exception is reported as a test failure or error after all tests complete. It can affect the test report but does not stop other tests since it runs last.
Click to reveal answer
beginner
Why use
@AfterAll instead of @AfterEach?@AfterAll runs once after all tests, good for expensive cleanup. @AfterEach runs after every test, which can be slower and redundant for shared resources.Click to reveal answer
What is the correct signature for an
@AfterAll method in JUnit 5 without @TestInstance?✗ Incorrect
@AfterAll methods must be static unless the class uses @TestInstance(TestInstance.Lifecycle.PER_CLASS).When is an
@AfterAll method executed?✗ Incorrect
@AfterAll runs once after all tests finish.Which annotation allows
@AfterAll methods to be non-static?✗ Incorrect
Using
@TestInstance(TestInstance.Lifecycle.PER_CLASS) lets @AfterAll methods be instance methods.What is a common use case for
@AfterAll?✗ Incorrect
@AfterAll is used to clean up resources shared by all tests.If an exception occurs in
@AfterAll, what happens?✗ Incorrect
Exceptions in
@AfterAll are reported after all tests complete.Explain the role and requirements of the
@AfterAll method in JUnit testing.Think about when and why you would close shared resources.
You got /3 concepts.
Describe the difference between
@AfterEach and @AfterAll annotations.Consider how often each method runs during testing.
You got /3 concepts.