A Guide to JUnit 5 Extensions
Last Updated :
23 Jul, 2025
In this article, we will explore JUnit 5 extensions, a powerful way to customize and extend the functionality of the JUnit 5 testing framework. These extensions allow you to include custom actions at any point in the test lifecycle, such as setup, teardown, and test execution.
JUnit 5 Extensions
JUnit 5 introduces a flexible extension model that can modify test execution behavior, inject dependencies, and manage shared resources. Extensions can be triggered at various stages of the testing lifecycle, including before and after all tests or individual tests and when specific events occur (e.g., exceptions, parameter resolution).
The JUnit 5 architecture consists of three components:
- JUnit Platform: A foundation for running and discovering tests.
- JUnit Jupiter: The programming and extension model in JUnit 5.
- JUnit Vintage: Allows backward compatibility for running JUnit 3 and 4 tests on the JUnit 5 platform.
JUnit 5 requires Java 8 or later to run, but it can still be used to test code written in older Java versions.
JUnit 5 = JUnit Jupiter + JUnit Vintage + JUnit Platform
JUnit Jupiter and Vintage
JUnit Jupiter is the programming and extension model that introduces new features and flexibility for writing tests and creating custom extensions. It uses a TestEngine to execute tests on the JUnit 5 platform.
JUnit Vintage allows older tests (written with JUnit 3 and 4) to run on JUnit 5. It requires JUnit 4.12 or later to be available in the classpath, ensuring seamless migration to JUnit 5 while maintaining the ability to run legacy tests.
Common Use Cases for JUnit 5 Extensions
JUnit 5 extensions can be used for:
- Customizing test behavior.
- Injecting dependencies during test execution.
- Modifying the flow of test execution.
- Setting up or tearing down shared resources.
Below are some key extensions provided by JUnit 5:
1. BeforeAllCallback
The BeforeAllCallback
extension allows executing code before all tests in a test class are run. It is commonly used for global setup tasks, such as initializing database connections or setting up external resources.
@ExtendWith(GlobalSetupExtension.class)
class MyTest {
@Test
void testExample() {
System.out.println("Running test example.");
}
}
Output:
Global setup before all tests.
Running test example.
2. AfterAllCallback
The AfterAllCallback
extension is executed after all the test methods in a test class have finished. This is useful for global cleanup tasks, such as closing database connections or releasing other resources.
@ExtendWith(GlobalTeardownExtension.class)
class MyTest {
@Test
void testExample() {
System.out.println("Running test example.");
}
}
Output:
Running test example.
Global teardown after all tests.
3. BeforeEachCallback
BeforeEachCallback
is executed before each test method in a test class. This extension is useful for preparing resources or resetting states before each test, such as resetting mocks or reinitializing test data.
@ExtendWith(SetupBeforeEachExtension.class)
class MyTest {
@Test
void testMethod1() {
System.out.println("Running test method 1.");
}
@Test
void testMethod2() {
System.out.println("Running test method 2.");
}
}
Output:
Setting up before each test.
Running test method 1.
Setting up before each test.
Running test method 2.
4. AfterEachCallback
The AfterEachCallback extension runs after each test method finishes. It is typically used for cleanup tasks such as closing file streams, resetting states, or clearing temporary data after each test execution.
@ExtendWith(TeardownAfterEachExtension.class)
class MyTest {
@Test
void testMethod1() {
System.out.println("Running test method 1.");
}
@Test
void testMethod2() {
System.out.println("Running test method 2.");
}
}
Output:
Running test method 1.
Cleaning up after each test.
Running test method 2.
Cleaning up after each test.
Conclusion
JUnit 5’s extension model provides a flexible way to customize and extend the test execution process. Extensions like BeforeAllCallback
, BeforeEachCallback
, AfterEachCallback
, and more, help with setting up resources, injecting dependencies, and cleaning up after tests. With these capabilities, you can create robust, maintainable, and efficient test suites tailored to your specific needs.
Similar Reads
A Guide to the @AutoClose Extension in JUnit 5 JUnit 5 is a powerful testing framework for Java applications that allows developers to write unit tests with ease. One of its standout features is the extension model, which enables the addition of new behaviors during test execution. Among these is the @AutoClose extension, a convenient way to aut
5 min read
JUnit 5 - Assertions JUnit is a robust and widely used testing framework for Java. This plays an important role in making sure the software is reliable. It provides developers with a great structure for creating and executing test cases. Junit features help to embrace a test-driven development approach, that ensures con
4 min read
How to Assert Exceptions in JUnit 4 and JUnit 5? In software testing, particularly in the unit tests, verifying that the code throws the expected exceptions under certain conditions is crucial. JUnit is the widely used testing framework for the Java, it allows us to assert exceptions using the different approaches depending on the version (JUnit 4
5 min read
Guide to JUnit 4 Rules In this article, we will explore the Rules feature provided by JUnit 4, a powerful mechanism to enhance unit tests. We will begin by understanding JUnit Rules and then explore some of the most common predefined rules that come with the JUnit library. Finally, we will see how to create and use custom
7 min read
JUnit 5 â Gradle Dependency JUnit is one of the most widely used unit testing frameworks for Java-based applications. Over the years, JUnit has evolved with different releases, including JUnit 3 and JUnit 4, which are now considered slightly outdated. The latest release, JUnit 5, was introduced to support modern Java features
7 min read
Introduction to JUnit 5 JUnit is a Testing Framework. The Junit 5 is the latest version of the testing framework, and it has a lot of features when compared with Junit 4. JUnit 5, also known as JUnit Jupiter. It introduces several new features and improvements over its predecessor, JUnit 4, making it more powerful and flex
8 min read