The @AfterEach method runs after every test to clean up or reset things. It helps keep tests independent and tidy.
You want to close a database connection after each test.
You need to reset shared variables or states after every test.
You want to delete temporary files created during a test.
You want to clear mock objects or reset configurations after each test.
Syntax
JUnit
@AfterEach
void methodName() {
// cleanup code here
}
The method must be void and take no parameters.
It runs after each test method in the class.
Examples
This prints a message after each test runs.
JUnit
@AfterEach
void cleanup() {
System.out.println("Cleaning up after test");
}
This resets a counter variable after every test.
JUnit
@AfterEach
void resetCounter() {
counter = 0;
}
Sample Program
This test class has two tests. Each test starts with total set to 5. After each test, total is reset to 0 by the @AfterEach method. The print statements show the order of execution.
JUnit
import org.junit.jupiter.api.*;
class CalculatorTest {
int total = 0;
@BeforeEach
void setup() {
total = 5;
System.out.println("Setup total to " + total);
}
@Test
void testAdd() {
total += 3;
Assertions.assertEquals(8, total);
System.out.println("Test add total: " + total);
}
@Test
void testSubtract() {
total -= 2;
Assertions.assertEquals(3, total);
System.out.println("Test subtract total: " + total);
}
@AfterEach
void cleanup() {
total = 0;
System.out.println("Cleanup total to " + total);
}
}
OutputSuccess
Important Notes
Use @AfterEach to avoid tests affecting each other.
If you want code to run once after all tests, use @AfterAll instead.
Summary
@AfterEach runs after every test method.
It helps clean up or reset things to keep tests independent.
Methods annotated with @AfterEach must be void and no-argument.
Practice
(1/5)
1. What is the main purpose of a method annotated with @AfterEach in JUnit?
easy
A. To define a test method itself
B. To run code before any test methods start
C. To run cleanup code after each test method executes
D. To skip a test method during execution
Solution
Step 1: Understand the role of @AfterEach
The @AfterEach annotation 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 C
Quick Check:
@AfterEach = cleanup after tests [OK]
Hint: Remember: @AfterEach runs after every test method [OK]
Common Mistakes:
Confusing @AfterEach with @BeforeEach
Thinking @AfterEach defines test methods
Assuming @AfterEach skips tests
2. Which of the following is the correct method signature for a method annotated with @AfterEach in JUnit?
easy
A. public int cleanup()
B. public void cleanup()
C. public void cleanup(String param)
D. public static void cleanup()
Solution
Step 1: Recall method requirements for @AfterEach
Methods with @AfterEach must 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.
A. Both tests pass because count resets after each test
B. Both tests fail because count is shared and not reset
C. testIncrement passes, testDecrement fails
D. testIncrement fails, testDecrement passes
Solution
Step 1: Understand @AfterEach effect on count
The reset() method sets count to 0 after each test method runs, so each test starts fresh.
Step 2: Analyze each test
In testIncrement(), count increments from 0 to 1, assertion matches. In testDecrement(), count decrements from 0 to -1, assertion matches. Both pass.
Final Answer:
Both tests pass because count resets after each test -> Option A
Quick Check:
@AfterEach resets state = tests independent [OK]
Hint: @AfterEach resets variables so tests don't affect each other [OK]
Common Mistakes:
Assuming count keeps value between tests
Forgetting @AfterEach runs after each test
Confusing @AfterEach with @BeforeEach
4. Identify the error in this JUnit test class:
class SampleTest {
@AfterEach
public static void cleanup() {
System.out.println("Cleaning up");
}
@Test
void testSomething() {
assertTrue(true);
}
}
medium
A. The @AfterEach method should not be static
B. The @AfterEach method must return boolean
C. The @Test method must be static
D. The @AfterEach method must have parameters
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 method cleanup() is declared static, which violates the rule.
Final Answer:
The @AfterEach method should not be static -> Option A
Quick Check:
@AfterEach methods cannot be static [OK]
Hint: @AfterEach methods must be instance, not static [OK]
Common Mistakes:
Making @AfterEach methods static
Adding parameters to @AfterEach methods
Changing return type from void
5. You have a test class where each test opens a database connection. You want to ensure the connection closes after each test to avoid resource leaks. Which is the best way to do this using @AfterEach?
hard
A. Do not close the connection; let JVM handle it
B. Call connection.close() at the start of each test method
C. Use @BeforeEach to close the connection before tests
D. Annotate a method that calls connection.close() with @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 @AfterEach to 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 D
Quick Check:
@AfterEach = cleanup resources after tests [OK]
Hint: Use @AfterEach to close resources after each test [OK]
Common Mistakes:
Closing resources before tests instead of after
Not closing resources causing leaks
Relying on JVM to close resources automatically
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.