What if your tests could clean up after themselves perfectly every single time, without you doing a thing?
Why @AfterEach method in JUnit? - Purpose & Use Cases
Imagine you run many tests on a web app, and after each test, you must manually reset the app state, clear caches, or close connections by hand.
This means opening the app, clicking buttons, or typing commands again and again after every test.
Doing this manually is slow and boring.
You might forget a step or do it wrong, causing tests to fail for unclear reasons.
It wastes time and makes your test results unreliable.
The @AfterEach method runs automatically after every test.
This means you write the cleanup code once, and JUnit runs it every time without you lifting a finger.
It keeps tests clean and independent, so one test does not affect another.
void test1() { /* test code */ cleanup(); } void test2() { /* test code */ cleanup(); }@AfterEach
void cleanup() { /* cleanup code */ }It enables reliable, fast, and automatic cleanup after each test, making your testing smooth and error-free.
Think of testing a shopping cart: after each test adds items, @AfterEach clears the cart automatically so the next test starts fresh.
@AfterEach runs cleanup code automatically after every test.
It prevents leftover data from one test affecting others.
Saves time and reduces human errors in testing.
Practice
@AfterEach in JUnit?Solution
Step 1: Understand the role of @AfterEach
The@AfterEachannotation marks methods that run after every test method to clean up or reset state.Step 2: Compare options with this role
Only To run cleanup code after each test method executes correctly describes this cleanup role after each test method.Final Answer:
To run cleanup code after each test method executes -> Option CQuick Check:
@AfterEach = cleanup after tests [OK]
- Confusing @AfterEach with @BeforeEach
- Thinking @AfterEach defines test methods
- Assuming @AfterEach skips tests
@AfterEach in JUnit?Solution
Step 1: Recall method requirements for @AfterEach
Methods with@AfterEachmust be void, no-argument, and instance methods (not static).Step 2: Check each option
public void cleanup() matches: public, void return, no parameters, instance method. Options B and C have wrong return type or parameters. public static void cleanup() is static, which is not allowed.Final Answer:
public void cleanup() -> Option BQuick Check:
@AfterEach method = void, no-args, non-static [OK]
- Adding parameters to @AfterEach methods
- Making @AfterEach methods static
- Using non-void return types
class ExampleTest {
int count = 0;
@AfterEach
void reset() {
count = 0;
}
@Test
void testIncrement() {
count++;
assertEquals(1, count);
}
@Test
void testDecrement() {
count--;
assertEquals(-1, count);
}
}What will be the result when running both tests?
Solution
Step 1: Understand @AfterEach effect on count
Thereset()method setscountto 0 after each test method runs, so each test starts fresh.Step 2: Analyze each test
IntestIncrement(), count increments from 0 to 1, assertion matches. IntestDecrement(), count decrements from 0 to -1, assertion matches. Both pass.Final Answer:
Both tests pass because count resets after each test -> Option AQuick Check:
@AfterEach resets state = tests independent [OK]
- Assuming count keeps value between tests
- Forgetting @AfterEach runs after each test
- Confusing @AfterEach with @BeforeEach
class SampleTest {
@AfterEach
public static void cleanup() {
System.out.println("Cleaning up");
}
@Test
void testSomething() {
assertTrue(true);
}
}Solution
Step 1: Check @AfterEach method signature rules
@AfterEach methods must be instance methods (non-static), void, and no-argument.Step 2: Identify the error in the code
The methodcleanup()is declared static, which violates the rule.Final Answer:
The @AfterEach method should not be static -> Option AQuick Check:
@AfterEach methods cannot be static [OK]
- Making @AfterEach methods static
- Adding parameters to @AfterEach methods
- Changing return type from void
@AfterEach?Solution
Step 1: Understand resource cleanup best practice
Resources like database connections should be closed after each test to avoid leaks and interference.Step 2: Apply @AfterEach for cleanup
Using a method annotated with@AfterEachto close the connection ensures it happens after every test, keeping tests independent and safe.Final Answer:
Annotate a method that calls connection.close() with @AfterEach -> Option DQuick Check:
@AfterEach = cleanup resources after tests [OK]
- Closing resources before tests instead of after
- Not closing resources causing leaks
- Relying on JVM to close resources automatically
Ready to prove it?
Pass the test and this pattern moves unknown
