- All Known Subinterfaces:
ExtensionContextInternal
ExtensionContext encapsulates the context in which the
current test or container is being executed.
Extensions are provided an instance of
ExtensionContext to perform their work.
This interface is not intended to be implemented by clients.
- Since:
- 5.0
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeInterfaceDescriptionstatic classANamespaceis used to provide a scope for data saved by extensions within aExtensionContext.Store.static interfaceStoreprovides methods for extensions to save and retrieve data.static enumStoreScopeis an enumeration of the different scopes forExtensionContext.Storeinstances. -
Method Summary
Modifier and TypeMethodDescriptionGet the configuration parameter stored under the specifiedkey.<T> Optional<T> getConfigurationParameter(String key, Function<String, T> transformer) Get and transform the configuration parameter stored under the specifiedkeyusing the specifiedtransformer.Get the display name for the current test or container.Get theAnnotatedElementcorresponding to the current extension context, if available.Get the enclosing test classes of the current test or container.Get anExecutableInvokerto invoke methods and constructors with support for dynamic resolution of parameters.Get the exception that was thrown during execution of the test or container associated with thisExtensionContext, if available.Get theExecutionModeassociated with the current test or container.Get the parent extension context, if available.default Class<?> Get the requiredClassassociated with the current test or container.default ObjectGet the required test instance associated with the current test or container.default TestInstancesGet the required test instances associated with the current test or container.default MethodGet the requiredMethodassociated with the current test or container.getRoot()Get the rootExtensionContext.getStore(ExtensionContext.Namespace namespace) Get theExtensionContext.Storefor the suppliedExtensionContext.Namespace.getStore(ExtensionContext.StoreScope scope, ExtensionContext.Namespace namespace) Returns the store for supplied scope and namespace.getTags()Get the set of all tags for the current test or container.Get theClassassociated with the current test or container, if available.Get the test instance associated with the current test or container, if available.Get theTestInstance.Lifecycleof the test instance associated with the current test or container, if available.Get the test instances associated with the current test or container, if available.Get theMethodassociated with the current test, if available.Get the unique ID of the current test or container.voidpublishDirectory(String name, ThrowingConsumer<Path> action) Publish a directory with the supplied name written by the supplied action and attach it to the current test or container.voidpublishFile(String name, MediaType mediaType, ThrowingConsumer<Path> action) Publish a file with the supplied name written by the supplied action and attach it to the current test or container.default voidpublishReportEntry(String value) Publish the specified value to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.default voidpublishReportEntry(String key, String value) Publish the specified key-value pair to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.voidpublishReportEntry(Map<String, String> map) Publish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.
-
Method Details
-
getParent
Optional<ExtensionContext> getParent()Get the parent extension context, if available.- Returns:
- an
Optionalcontaining the parent; nevernullbut potentially empty - See Also:
-
getRoot
ExtensionContext getRoot()Get the rootExtensionContext.- Returns:
- the root extension context; never
nullbut potentially thisExtensionContext - See Also:
-
getUniqueId
String getUniqueId()Get the unique ID of the current test or container.- Returns:
- the unique ID of the test or container; never
nullor blank
-
getDisplayName
String getDisplayName()Get the display name for the current test or container.The display name is either a default name or a custom name configured via
@DisplayName.For details on default display names consult the Javadoc for
TestInfo.getDisplayName().Note that display names are typically used for test reporting in IDEs and build tools and may contain spaces, special characters, and even emoji.
- Returns:
- the display name of the test or container; never
nullor blank
-
getTags
Get the set of all tags for the current test or container.Tags may be declared directly on the test element or inherited from an outer context.
- Returns:
- the set of tags for the test or container; never
nullbut potentially empty
-
getElement
Optional<AnnotatedElement> getElement()Get theAnnotatedElementcorresponding to the current extension context, if available.For example, if the current extension context encapsulates a test class, test method, test factory method, or test template method, the annotated element will be the corresponding
ClassorMethodreference.Favor this method over more specific methods whenever the
AnnotatedElementAPI suits the task at hand — for example, when looking up annotations regardless of concrete element type.- Returns:
- an
Optionalcontaining theAnnotatedElement; nevernullbut potentially empty - See Also:
-
getTestClass
Get theClassassociated with the current test or container, if available.- Returns:
- an
Optionalcontaining the class; nevernullbut potentially empty - See Also:
-
getEnclosingTestClasses
Get the enclosing test classes of the current test or container.This method is useful to look up annotations on nested test classes and their enclosing runtime types:
AnnotationSupport.findAnnotation( extensionContext.getRequiredTestClass(), MyAnnotation.class, extensionContext.getEnclosingTestClasses() );- Returns:
- an empty list if there is no class associated with the current
test or container or when it is not nested; otherwise, a list containing
the enclosing test classes in order from outermost to innermost; never
null - Since:
- 5.12.1
- See Also:
-
getRequiredTestClass
Get the requiredClassassociated with the current test or container.Use this method as an alternative to
getTestClass()for use cases in which the test class is required to be present.- Returns:
- the test class; never
null - Throws:
PreconditionViolationException- if the test class is not present in thisExtensionContext
-
getTestInstanceLifecycle
Get theTestInstance.Lifecycleof the test instance associated with the current test or container, if available.- Returns:
- an
Optionalcontaining the test instanceLifecycle; nevernullbut potentially empty - Since:
- 5.1
- See Also:
-
getTestInstance
Get the test instance associated with the current test or container, if available.- Returns:
- an
Optionalcontaining the test instance; nevernullbut potentially empty - See Also:
-
getRequiredTestInstance
Get the required test instance associated with the current test or container.Use this method as an alternative to
getTestInstance()for use cases in which the test instance is required to be present.- Returns:
- the test instance; never
null - Throws:
PreconditionViolationException- if the test instance is not present in thisExtensionContext- See Also:
-
getTestInstances
Get the test instances associated with the current test or container, if available.While top-level tests only have a single test instance, nested tests have one additional instance for each enclosing test class.
- Returns:
- an
Optionalcontaining the test instances; nevernullbut potentially empty - Since:
- 5.4
- See Also:
-
getRequiredTestInstances
Get the required test instances associated with the current test or container.Use this method as an alternative to
getTestInstances()for use cases in which the test instances are required to be present.- Returns:
- the test instances; never
null - Throws:
PreconditionViolationException- if the test instances are not present in thisExtensionContext- Since:
- 5.4
-
getTestMethod
Get theMethodassociated with the current test, if available.- Returns:
- an
Optionalcontaining the method; nevernullbut potentially empty - See Also:
-
getRequiredTestMethod
Get the requiredMethodassociated with the current test or container.Use this method as an alternative to
getTestMethod()for use cases in which the test method is required to be present.- Returns:
- the test method; never
null - Throws:
PreconditionViolationException- if the test method is not present in thisExtensionContext
-
getExecutionException
Get the exception that was thrown during execution of the test or container associated with thisExtensionContext, if available.This method is typically used for logging and tracing purposes. If you wish to actually handle an exception thrown during test execution, implement the
TestExecutionExceptionHandlerAPI.Unlike the exception passed to a
TestExecutionExceptionHandler, an execution exception returned by this method can be any exception thrown during the invocation of a@Testmethod, its surrounding@BeforeEachand@AfterEachmethods, or a test-levelExtension. Similarly, if thisExtensionContextrepresents a test class, the execution exception returned by this method can be any exception thrown in a@BeforeAllorAfterAllmethod or a class-levelExtension.Note, however, that this method will never return an exception swallowed by a
TestExecutionExceptionHandler. Furthermore, if multiple exceptions have been thrown during test execution, the exception returned by this method will be the first such exception with all additional exceptions suppressed in the first one.- Returns:
- an
Optionalcontaining the exception thrown; nevernullbut potentially empty if test execution has not (yet) resulted in an exception
-
getConfigurationParameter
Get the configuration parameter stored under the specifiedkey.If no such key is present in the
ConfigurationParametersfor the JUnit Platform, an attempt will be made to look up the value as a JVM system property. If no such system property exists, an attempt will be made to look up the value in the JUnit Platform properties file.- Parameters:
key- the key to look up; nevernullor blank- Returns:
- an
Optionalcontaining the value; nevernullbut potentially empty - Since:
- 5.1
- See Also:
-
getConfigurationParameter
@API(status=STABLE, since="5.10") <T> Optional<T> getConfigurationParameter(String key, Function<String, T> transformer) Get and transform the configuration parameter stored under the specifiedkeyusing the specifiedtransformer.If no such key is present in the
ConfigurationParametersfor the JUnit Platform, an attempt will be made to look up the value as a JVM system property. If no such system property exists, an attempt will be made to look up the value in the JUnit Platform properties file.In case the transformer throws an exception, it will be wrapped in a
JUnitExceptionwith a helpful message.- Parameters:
key- the key to look up; nevernullor blanktransformer- the transformer to apply in case a value is found; nevernull- Returns:
- an
Optionalcontaining the value; nevernullbut potentially empty - Since:
- 5.7
- See Also:
-
publishReportEntry
Publish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.- Parameters:
map- the key-value pairs to be published; nevernull; keys and values within entries in the map also must not benullor blank- See Also:
-
publishReportEntry
Publish the specified key-value pair to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.- Parameters:
key- the key of the published pair; nevernullor blankvalue- the value of the published pair; nevernullor blank- See Also:
-
publishReportEntry
Publish the specified value to be consumed by anorg.junit.platform.engine.EngineExecutionListenerin order to supply additional information to the reporting infrastructure.This method delegates to
publishReportEntry(String, String), supplying"value"as the key and the suppliedvalueargument as the value.- Parameters:
value- the value to be published; nevernullor blank- Since:
- 5.3
- See Also:
-
publishFile
@API(status=MAINTAINED, since="5.13.3") void publishFile(String name, MediaType mediaType, ThrowingConsumer<Path> action) Publish a file with the supplied name written by the supplied action and attach it to the current test or container.The file will be resolved in the report output directory prior to invoking the supplied action.
- Parameters:
name- the name of the file to be attached; nevernullor blank and must not contain any path separatorsmediaType- the media type of the file; nevernull; useMediaType.APPLICATION_OCTET_STREAMif unknownaction- the action to be executed to write the file; nevernull- Since:
- 5.12
- See Also:
-
publishDirectory
@API(status=MAINTAINED, since="5.13.3") void publishDirectory(String name, ThrowingConsumer<Path> action) Publish a directory with the supplied name written by the supplied action and attach it to the current test or container.The directory will be resolved and created in the report output directory prior to invoking the supplied action, if it doesn't already exist.
- Parameters:
name- the name of the directory to be attached; nevernullor blank and must not contain any path separatorsaction- the action to be executed to write the file; nevernull- Since:
- 5.12
- See Also:
-
getStore
Get theExtensionContext.Storefor the suppliedExtensionContext.Namespace.Use
getStore(Namespace.GLOBAL)to get the default, globalExtensionContext.Namespace.A store is bound to its extension context lifecycle. When an extension context lifecycle ends it closes its associated store. All stored values that are instances of
ExtensionContext.Store.CloseableResourceare notified by invoking theirclose()methods.- Parameters:
namespace- theNamespaceto get the store for; nevernull- Returns:
- the store in which to put and get objects for other invocations
working in the same namespace; never
null - See Also:
-
getStore
@API(status=MAINTAINED, since="5.13.3") ExtensionContext.Store getStore(ExtensionContext.StoreScope scope, ExtensionContext.Namespace namespace) Returns the store for supplied scope and namespace.If
scopeisEXTENSION_CONTEXT, the store behaves exactly like the one returned bygetStore(Namespace). If thescopeisLAUNCHER_SESSIONorEXECUTION_REQUEST, all stored values that are instances ofAutoCloseableare notified by invoking theirclose()methods when the scope is closed.- Since:
- 5.13
- See Also:
-
getExecutionMode
Get theExecutionModeassociated with the current test or container.- Returns:
- the
ExecutionModeof the test; nevernull - Since:
- 5.8.1
- See Also:
-
getExecutableInvoker
Get anExecutableInvokerto invoke methods and constructors with support for dynamic resolution of parameters.- Since:
- 5.9
-