BeforeEachCallback and AfterEachCallback help run code before and after each test. This keeps tests clean and avoids repeating setup or cleanup steps.
0
0
BeforeEachCallback and AfterEachCallback in JUnit
Introduction
You want to open a database connection before each test and close it after.
You need to reset some data or state before every test runs.
You want to log information before and after each test for debugging.
You want to prepare test data fresh for each test case.
You want to clean temporary files after each test finishes.
Syntax
JUnit
public class MyExtension implements BeforeEachCallback, AfterEachCallback {
@Override
public void beforeEach(ExtensionContext context) throws Exception {
// code to run before each test
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
// code to run after each test
}
}Implement both interfaces in one class to run code before and after each test.
The ExtensionContext gives info about the test being run.
Examples
This example prints the test name before each test starts.
JUnit
public class MyExtension implements BeforeEachCallback { @Override public void beforeEach(ExtensionContext context) throws Exception { System.out.println("Starting test: " + context.getDisplayName()); } }
This example prints the test name after each test finishes.
JUnit
public class MyExtension implements AfterEachCallback { @Override public void afterEach(ExtensionContext context) throws Exception { System.out.println("Finished test: " + context.getDisplayName()); } }
This example shows both setup and cleanup messages around each test.
JUnit
public class MyExtension implements BeforeEachCallback, AfterEachCallback { @Override public void beforeEach(ExtensionContext context) throws Exception { System.out.println("Setup before " + context.getDisplayName()); } @Override public void afterEach(ExtensionContext context) throws Exception { System.out.println("Cleanup after " + context.getDisplayName()); } }
Sample Program
This test class uses MyExtension to print messages before and after each test. It runs two simple tests that print their own messages.
JUnit
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.RegisterExtension; import org.junit.jupiter.api.Test; public class ExampleTest { static class MyExtension implements BeforeEachCallback, AfterEachCallback { @Override public void beforeEach(ExtensionContext context) throws Exception { System.out.println("Before test: " + context.getDisplayName()); } @Override public void afterEach(ExtensionContext context) throws Exception { System.out.println("After test: " + context.getDisplayName()); } } @RegisterExtension static MyExtension extension = new MyExtension(); @Test void testOne() { System.out.println("Running testOne"); } @Test void testTwo() { System.out.println("Running testTwo"); } }
OutputSuccess
Important Notes
Use @RegisterExtension to apply your extension in the test class.
BeforeEachCallback runs before each test method, AfterEachCallback runs after.
Extensions help keep test code clean by handling setup and cleanup in one place.
Summary
BeforeEachCallback and AfterEachCallback run code before and after each test.
They help avoid repeating setup and cleanup code in tests.
Use ExtensionContext to get info about the current test.