Close

JUnit 5 - Extension Model Overview

[Last Updated: Dec 31, 2025]

The JUnit Jupiter extension model allows developers to extend the behavior of the test engine at various points in the test execution lifecycle. Unlike the Runner and Rule APIs in JUnit 4, the Jupiter extension model is based on a single, coherent concept: the Extension interface.

Extensions can be registered declaratively via @ExtendWith, programmatically, or automatically via Java's ServiceLoader. Below is an overview of the primary extension points available in JUnit Jupiter.

Available Extension Points

While the list of Extension subclasses in JUnit Jupiter is extensive, most of them fall into two categories: Internal Engine Implementations (which you use via annotations like @EnabledOnOs) and Public Extension Points (which you implement to create custom logic).

The following are important Public Extension Points.

  • ExecutionCondition: Used to programmatically enable or disable test execution.
  • BeforeAllCallback: Defines behavior to be executed before all tests in the current container.
  • BeforeEachCallback: Defines behavior to be executed before each test method.
  • BeforeTestExecutionCallback: Executed immediately before a test method but after @BeforeEach.
  • AfterTestExecutionCallback: Executed immediately after a test method but before @AfterEach.
  • AfterEachCallback: Executed after each test method.
  • AfterAllCallback: Executed after all tests in the current container.
  • ParameterResolver: Used to resolve parameters for constructors or test methods at runtime.
  • InvocationInterceptor: Used to intercept calls to test methods.
  • TestExecutionExceptionHandler: Allows for custom handling of exceptions thrown during test execution.
  • LifecycleMethodExecutionExceptionHandler: Used to handle exceptions that occur within lifecycle methods.
  • TestInstanceFactory : Used to dynamically create test class instances.
  • TestInstancePostProcessor: Used to post-process test instances (e.g., performing dependency injection).
  • TestInstancePreDestroyCallback: It is the final cleanup hook for a specific test instance.
  • TestWatcher: Used to monitor the results of test execution

Understanding these extension points is the key to mastering JUnit 5. They provide a granular, non-invasive way to inject logic across different phases of the testing lifecycle, replacing the more rigid structures found in previous versions of the framework.

In this tutorial series, we will explore examples of key extension points.

See Also