Interface ExtensionContext
-
- All Known Implementing Classes:
ClassExtensionContext,JupiterEngineExtensionContext,MethodExtensionContext
@API(status=STABLE, since="5.0") public interface ExtensionContext
ExtensionContextencapsulates the context in which the current test or container is being executed.Extensionsare provided an instance ofExtensionContextto perform their work.- Since:
- 5.0
- See Also:
ExtensionContext.Store,ExtensionContext.Namespace
-
-
Nested Class Summary
Nested Classes Modifier and Type Interface Description static classExtensionContext.NamespaceANamespaceis used to provide a scope for data saved by extensions within aExtensionContext.Store.static interfaceExtensionContext.StoreStoreprovides methods for extensions to save and retrieve data.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description Optional<String>getConfigurationParameter(String key)Get the configuration parameter stored under the specifiedkey.StringgetDisplayName()Get the display name for the current test or container.Optional<AnnotatedElement>getElement()Get theAnnotatedElementcorresponding to the current extension context, if available.Optional<Throwable>getExecutionException()Get the exception that was thrown during execution of the test or container associated with thisExtensionContext, if available.Optional<ExtensionContext>getParent()Get the parent extension context, if available.default Class<?>getRequiredTestClass()Get the requiredClassassociated with the current test or container.default ObjectgetRequiredTestInstance()Get the required test instance associated with the current test or container.default MethodgetRequiredTestMethod()Get the requiredMethodassociated with the current test or container.ExtensionContextgetRoot()Get the rootExtensionContext.ExtensionContext.StoregetStore(ExtensionContext.Namespace namespace)Get theExtensionContext.Storefor the suppliedExtensionContext.Namespace.Set<String>getTags()Get the set of all tags for the current test or container.Optional<Class<?>>getTestClass()Get theClassassociated with the current test or container, if available.Optional<Object>getTestInstance()Get the test instance associated with the current test or container, if available.Optional<TestInstance.Lifecycle>getTestInstanceLifecycle()Get theTestInstance.Lifecycleof the test instance associated with the current test or container, if available.Optional<Method>getTestMethod()Get theMethodassociated with the current test, if available.StringgetUniqueId()Get the unique ID of the current test or container.default voidpublishReportEntry(String key, String value)Publish the specified key-value pair to be consumed by anorg.junit.platform.engine.EngineExecutionListener.voidpublishReportEntry(Map<String,String> map)Publish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListener.
-
-
-
Method Detail
-
getParent
Optional<ExtensionContext> getParent()
Get the parent extension context, if available.- Returns:
- an
Optionalcontaining the parent; nevernullbut potentially empty - See Also:
getRoot()
-
getRoot
ExtensionContext getRoot()
Get the rootExtensionContext.- Returns:
- the root extension context; never
nullbut potentially thisExtensionContext - See Also:
getParent()
-
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
Set<String> 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(),getTestMethod()
-
getTestClass
Optional<Class<?>> getTestClass()
Get theClassassociated with the current test or container, if available.- Returns:
- an
Optionalcontaining the class; nevernullbut potentially empty - See Also:
getRequiredTestClass()
-
getRequiredTestClass
default Class<?> 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
@API(status=STABLE, since="5.1") Optional<TestInstance.Lifecycle> 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:
TestInstance
-
getTestInstance
Optional<Object> 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()
-
getRequiredTestInstance
default Object 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
-
getTestMethod
Optional<Method> getTestMethod()
Get theMethodassociated with the current test, if available.- Returns:
- an
Optionalcontaining the method; nevernullbut potentially empty - See Also:
getRequiredTestMethod()
-
getRequiredTestMethod
default Method 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
Optional<Throwable> 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
@API(status=STABLE, since="5.1") Optional<String> getConfigurationParameter(String key)
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:
System.getProperty(String),ConfigurationParameters
-
publishReportEntry
void publishReportEntry(Map<String,String> map)
Publish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListener.- Parameters:
map- the key-value pairs to be published; nevernull; keys and values within entries in the map also must not benullor blank
-
publishReportEntry
default void publishReportEntry(String key, String value)
Publish the specified key-value pair to be consumed by anorg.junit.platform.engine.EngineExecutionListener.- Parameters:
key- the key of the published pair; nevernullor blankvalue- the value of the published pair; nevernullor blank
-
getStore
ExtensionContext.Store getStore(ExtensionContext.Namespace namespace)
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:
ExtensionContext.Namespace.GLOBAL
-
-