@API(status=STABLE, since="5.0") public interface ExtensionContext
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.
ExtensionContext.Store
,
ExtensionContext.Namespace
Modifier and Type | Interface and Description |
---|---|
static class |
ExtensionContext.Namespace
A
Namespace is used to provide a scope for data saved by
extensions within a ExtensionContext.Store . |
static interface |
ExtensionContext.Store
Store provides methods for extensions to save and retrieve data. |
Modifier and Type | Method and Description |
---|---|
String |
getDisplayName()
Get the display name for the current test or container.
|
Optional<AnnotatedElement> |
getElement()
Get the
AnnotatedElement corresponding 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 this
ExtensionContext , if available. |
Optional<ExtensionContext> |
getParent()
Get the parent extension context, if available.
|
default Class<?> |
getRequiredTestClass()
Get the required
Class associated with the current test
or container. |
default Object |
getRequiredTestInstance()
Get the required test instance associated with the current test
or container.
|
default Method |
getRequiredTestMethod()
Get the required
Method associated with the current test
or container. |
ExtensionContext |
getRoot()
Get the root
ExtensionContext . |
ExtensionContext.Store |
getStore(ExtensionContext.Namespace namespace)
Get the
ExtensionContext.Store for the supplied ExtensionContext.Namespace . |
Set<String> |
getTags()
Get the set of all tags for the current test or container.
|
Optional<Class<?>> |
getTestClass()
Get the
Class associated 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<Method> |
getTestMethod()
Get the
Method associated with the current test, if available. |
String |
getUniqueId()
Get the unique ID of the current test or container.
|
void |
publishReportEntry(Map<String,String> map)
Publish a map of key-value pairs to be consumed by an
org.junit.platform.engine.EngineExecutionListener . |
default void |
publishReportEntry(String key,
String value)
Publish the specified key-value pair to be consumed by an
org.junit.platform.engine.EngineExecutionListener . |
Optional<ExtensionContext> getParent()
Optional
containing the parent; never null
but
potentially emptygetRoot()
ExtensionContext getRoot()
ExtensionContext
.null
but potentially
this ExtensionContext
getParent()
String getUniqueId()
null
or blankString getDisplayName()
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.
null
or blankSet<String> getTags()
Tags may be declared directly on the test element or inherited from an outer context.
null
but
potentially emptyOptional<AnnotatedElement> getElement()
AnnotatedElement
corresponding 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 Class
or Method
reference.
Favor this method over more specific methods whenever the
AnnotatedElement
API suits the task at hand — for example,
when looking up annotations regardless of concrete element type.
Optional
containing the AnnotatedElement
;
never null
but potentially emptygetTestClass()
,
getTestMethod()
Optional<Class<?>> getTestClass()
Class
associated with the current test or container,
if available.Optional
containing the class; never null
but
potentially emptygetRequiredTestClass()
default Class<?> getRequiredTestClass()
Class
associated 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.
null
PreconditionViolationException
- if the test class is not present
in this ExtensionContext
Optional<Object> getTestInstance()
Optional
containing the test instance; never
null
but potentially emptygetRequiredTestInstance()
default Object getRequiredTestInstance()
Use this method as an alternative to getTestInstance()
for use
cases in which the test instance is required to be present.
null
PreconditionViolationException
- if the test instance is not present
in this ExtensionContext
Optional<Method> getTestMethod()
Method
associated with the current test, if available.Optional
containing the method; never null
but
potentially emptygetRequiredTestMethod()
default Method getRequiredTestMethod()
Method
associated 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.
null
PreconditionViolationException
- if the test method is not present
in this ExtensionContext
Optional<Throwable> getExecutionException()
ExtensionContext
, 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 TestExecutionExceptionHandler
API.
Unlike the exception passed to a TestExecutionExceptionHandler
,
an execution exception returned by this method can be any
exception thrown during the invocation of a @Test
method, its
surrounding @BeforeEach
and @AfterEach
methods, or a
test-level Extension
. Similarly, if this ExtensionContext
represents a test class, the execution exception returned by
this method can be any exception thrown in a @BeforeAll
or
AfterAll
method or a class-level Extension
.
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.
Optional
containing the exception thrown; never
null
but potentially empty if test execution has not (yet)
resulted in an exceptionvoid publishReportEntry(Map<String,String> map)
org.junit.platform.engine.EngineExecutionListener
.map
- the key-value pairs to be published; never null
;
keys and values within entries in the map also must not be
null
or blankdefault void publishReportEntry(String key, String value)
org.junit.platform.engine.EngineExecutionListener
.key
- the key of the published pair; never null
or blankvalue
- the value of the published pair; never null
or blankExtensionContext.Store getStore(ExtensionContext.Namespace namespace)
ExtensionContext.Store
for the supplied ExtensionContext.Namespace
.
Use getStore(Namespace.GLOBAL)
to get the default, global ExtensionContext.Namespace
.
namespace
- the Namespace
to get the store for; never null
null
ExtensionContext.Namespace.GLOBAL