Class MethodExtensionContext
- java.lang.Object
-
- org.junit.jupiter.engine.descriptor.MethodExtensionContext
-
- All Implemented Interfaces:
AutoCloseable,ExtensionContext
@API(status=INTERNAL, since="5.0") public final class MethodExtensionContext extends Object
- Since:
- 5.0
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.ExtensionContext
ExtensionContext.Namespace, ExtensionContext.Store
-
-
Constructor Summary
Constructors Constructor Description MethodExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, TestMethodTestDescriptor testDescriptor, ConfigurationParameters configurationParameters, Object testInstance, ThrowableCollector throwableCollector)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()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.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.protected TgetTestDescriptor()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.voidpublishReportEntry(Map<String,String> values)Publish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListener.-
Methods inherited from interface org.junit.jupiter.api.extension.ExtensionContext
getRequiredTestClass, getRequiredTestInstance, getRequiredTestMethod, publishReportEntry
-
-
-
-
Constructor Detail
-
MethodExtensionContext
public MethodExtensionContext(ExtensionContext parent, EngineExecutionListener engineExecutionListener, TestMethodTestDescriptor testDescriptor, ConfigurationParameters configurationParameters, Object testInstance, ThrowableCollector throwableCollector)
-
-
Method Detail
-
getElement
public Optional<AnnotatedElement> getElement()
Description copied from interface:ExtensionContextGet 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:
ExtensionContext.getTestClass(),ExtensionContext.getTestMethod()
-
getTestClass
public Optional<Class<?>> getTestClass()
Description copied from interface:ExtensionContextGet theClassassociated with the current test or container, if available.- Returns:
- an
Optionalcontaining the class; nevernullbut potentially empty - See Also:
ExtensionContext.getRequiredTestClass()
-
getTestInstanceLifecycle
public Optional<TestInstance.Lifecycle> getTestInstanceLifecycle()
Description copied from interface:ExtensionContextGet theTestInstance.Lifecycleof the test instance associated with the current test or container, if available.- Returns:
- an
Optionalcontaining the test instanceLifecycle; nevernullbut potentially empty - See Also:
TestInstance
-
getTestInstance
public Optional<Object> getTestInstance()
Description copied from interface:ExtensionContextGet the test instance associated with the current test or container, if available.- Returns:
- an
Optionalcontaining the test instance; nevernullbut potentially empty - See Also:
ExtensionContext.getRequiredTestInstance()
-
getTestMethod
public Optional<Method> getTestMethod()
Description copied from interface:ExtensionContextGet theMethodassociated with the current test, if available.- Returns:
- an
Optionalcontaining the method; nevernullbut potentially empty - See Also:
ExtensionContext.getRequiredTestMethod()
-
getExecutionException
public Optional<Throwable> getExecutionException()
Description copied from interface:ExtensionContextGet 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
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
getUniqueId
public String getUniqueId()
Description copied from interface:ExtensionContextGet the unique ID of the current test or container.- Specified by:
getUniqueIdin interfaceExtensionContext- Returns:
- the unique ID of the test or container; never
nullor blank
-
getDisplayName
public String getDisplayName()
Description copied from interface:ExtensionContextGet 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.
- Specified by:
getDisplayNamein interfaceExtensionContext- Returns:
- the display name of the test or container; never
nullor blank
-
publishReportEntry
public void publishReportEntry(Map<String,String> values)
Description copied from interface:ExtensionContextPublish a map of key-value pairs to be consumed by anorg.junit.platform.engine.EngineExecutionListener.- Specified by:
publishReportEntryin interfaceExtensionContext- Parameters:
values- the key-value pairs to be published; nevernull; keys and values within entries in the map also must not benullor blank
-
getParent
public Optional<ExtensionContext> getParent()
Description copied from interface:ExtensionContextGet the parent extension context, if available.- Specified by:
getParentin interfaceExtensionContext- Returns:
- an
Optionalcontaining the parent; nevernullbut potentially empty - See Also:
ExtensionContext.getRoot()
-
getRoot
public ExtensionContext getRoot()
Description copied from interface:ExtensionContextGet the rootExtensionContext.- Specified by:
getRootin interfaceExtensionContext- Returns:
- the root extension context; never
nullbut potentially thisExtensionContext - See Also:
ExtensionContext.getParent()
-
getTestDescriptor
protected T getTestDescriptor()
-
getStore
public ExtensionContext.Store getStore(ExtensionContext.Namespace namespace)
Description copied from interface:ExtensionContextGet 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.- Specified by:
getStorein interfaceExtensionContext- 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
-
getTags
public Set<String> getTags()
Description copied from interface:ExtensionContextGet 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.
- Specified by:
getTagsin interfaceExtensionContext- Returns:
- the set of tags for the test or container; never
nullbut potentially empty
-
getConfigurationParameter
public Optional<String> getConfigurationParameter(String key)
Description copied from interface:ExtensionContextGet 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.- Specified by:
getConfigurationParameterin interfaceExtensionContext- Parameters:
key- the key to look up; nevernullor blank- Returns:
- an
Optionalcontaining the value; nevernullbut potentially empty - See Also:
System.getProperty(String),ConfigurationParameters
-
-