0
0
JUnittesting~5 mins

@AfterAll method in JUnit - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
Apublic static void methodName()
Bpublic void methodName()
Cprivate void methodName()
Dprotected void methodName()
When is an @AfterAll method executed?
ABefore each test method
BOnce after all test methods in the class
CAfter each test method
DBefore all test methods in the class
Which annotation allows @AfterAll methods to be non-static?
A@TestInstance(TestInstance.Lifecycle.PER_CLASS)
B@TestInstance(Lifecycle.PER_METHOD)
C@BeforeAll
D@Test
What is a common use case for @AfterAll?
AInitialize test data before each test
BSkip a test method
CRun a test multiple times
DClean up shared resources after all tests
If an exception occurs in @AfterAll, what happens?
ATests stop immediately
BException is ignored
CException is reported after all tests
DTests rerun automatically
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.