Close

JUnit 5 - ExtensionContext in JUnit Jupiter

[Last Updated: Jan 5, 2026]

The ExtensionContext is the primary interface used by JUnit Jupiter extensions to access the current test's execution state, metadata, and provide a mechanism for data persistence via a Store. It encapsulates information about the test hierarchy and provides access to the reflected elements.

Definition of ExtensionContext

Version: 6.0.1
 package org.junit.jupiter.api.extension;
 @API(status = STABLE, since = "5.0")
 public interface ExtensionContext {
     Optional<ExtensionContext> getParent(); 
     ExtensionContext getRoot(); 
     String getUniqueId(); 
     String getDisplayName(); 
     Set<String> getTags(); 
     Optional<AnnotatedElement> getElement(); 
     Optional<Class<?>> getTestClass(); 
     default Class<?> getRequiredTestClass(); 
     List<Class<?>> getEnclosingTestClasses(); 
     Optional<Lifecycle> getTestInstanceLifecycle(); 
     Optional<Object> getTestInstance(); 
     default Object getRequiredTestInstance(); 
     Optional<TestInstances> getTestInstances(); 
     default TestInstances getRequiredTestInstances(); 
     Optional<Method> getTestMethod(); 
     default Method getRequiredTestMethod(); 
     Optional<Throwable> getExecutionException(); 
     Optional<String> getConfigurationParameter(String key); 
     <T> Optional<T> getConfigurationParameter(String key, 
                                         Function<? super String,
                                             ? extends @Nullable T> transformer);
     void publishReportEntry(Map<String, String> map); 
     default void publishReportEntry(String key, 
                                     String value);
     default void publishReportEntry(String value); 
     default void publishFile(String name, 
                              org.junit.jupiter.api.extension.MediaType mediaType,
                              ThrowingConsumer<Path> action);
     void publishFile(String name, 
                      MediaType mediaType,
                      ThrowingConsumer<Path> action);
     void publishDirectory(String name, 
                           ThrowingConsumer<Path> action);
     Store getStore(Namespace namespace); 
     Store getStore(StoreScope scope, 
                    Namespace namespace);
     ExecutionMode getExecutionMode(); 
     ExecutableInvoker getExecutableInvoker(); 
     interface Store { 
         Object get(Object key); 
         <V> V get(Object key, 
                   Class<V> requiredType);
         default <V> V getOrDefault(Object key, 
                                    Class<V> requiredType,
                                    V defaultValue);
         default <V> V getOrComputeIfAbsent(Class<V> type); 
         default <V> V computeIfAbsent(Class<V> type); 
         <K, V extends @Nullable Object> Object getOrComputeIfAbsent(K key, 
                                         Function<? super K,
                                             ? extends V> defaultCreator);
         <K, V> Object computeIfAbsent(K key, 
                                       Function<? super K,
                                           ? extends V> defaultCreator);
         <K, V extends @Nullable Object> V getOrComputeIfAbsent(K key, 
                                         Function<? super K,
                                             ? extends V> defaultCreator,
                                         Class<V> requiredType);
         <K, V> V computeIfAbsent(K key, 
                                  Function<? super K, ? extends V> defaultCreator,
                                  Class<V> requiredType);
         void put(Object key, 
                  @Nullable Object value);
         Object remove(Object key); 
         <V> V remove(Object key, 
                      Class<V> requiredType);
     }
     final class Namespace { 
         ...
     }
     @API(status = EXPERIMENTAL, since = "6.0")
     enum StoreScope { 
         LAUNCHER_SESSION, 
         EXECUTION_REQUEST, 
         EXTENSION_CONTEXT 
     }
 }

ExtensionContext methods

The API is organized into several functional groups to manage the lifecycle and data flow of extensions effectively.

1. State Persistence

Scoped storage system to persist state:

  • getStore(Namespace namespace): Store associated with the given namespace
  • getStore(StoreScope scope, Namespace namespace): Store from a specific scope with the given namespace.

2. Hierarchy & Identification

Used to navigate the test tree and identify the current execution scope:

  • getParent() / getRoot(): Navigate context levels.
  • getUniqueId(): Technical ID for the node.
  • getDisplayName(): Human-readable name.

3. Metadata & Test Discovery

Accessing the underlying Java code structures:

  • getElement(): The AnnotatedElement in scope.
  • getTestMethod() / getRequiredTestMethod(): The specific test method being executed.
  • getTestClass() / getRequiredTestClass(): The specific test class being executed.
  • getTags(): The tags associated with the current test.
  • getExecutionMode(): Parallel vs. Sequential execution status.

3. Test Instances & Lifecycle

Accessing the physical test objects:

  • getTestInstance() / getRequiredTestInstance(): The current test class instance.
  • getTestInstances() / getRequiredTestInstances(): Instances of @Nested classes and outer class.
  • getTestInstanceLifecycle(): The lifecycle mode for test instances (PER_METHOD or PER_CLASS)

4. Test Failures & Exceptions

Handling test failures and exceptions:

  • getExecutionException(): The exception thrown during test execution.

5. Programmatic Method Execution

Invoking methods or constructors using JUnit's native invocation pipeline:

  • getExecutableInvoker(): Returns ExecutableInvoker for programmatic method and constructor invocation.

6. Publishing Reports & Artifacts

Publish reports, files, and directories:

  • publishReportEntry(....): Publish keys/values to the test report.
  • publishFile(....): Publish report file.
  • publishDirectory(....): Publish a report directory.

7. Accessing Configuration Parameters

Read configuration values from various sources:

  • getConfigurationParameter(....): Configuration parameter value.



In next tutorials we will see example of each above methods of ExtensionContext.

See Also