Interface ExtensionContext

    • Method Summary

      All Methods Instance Methods Abstract Methods Default Methods 
      Modifier and Type Method Description
      java.util.Optional<java.lang.String> getConfigurationParameter​(java.lang.String key)
      Get the configuration parameter stored under the specified key.
      java.lang.String getDisplayName​()
      Get the display name for the current test or container.
      java.util.Optional<java.lang.reflect.AnnotatedElement> getElement​()
      Get the AnnotatedElement corresponding to the current extension context, if available.
      java.util.Optional<java.lang.Throwable> getExecutionException​()
      Get the exception that was thrown during execution of the test or container associated with this ExtensionContext, if available.
      java.util.Optional<ExtensionContext> getParent​()
      Get the parent extension context, if available.
      default java.lang.Class<?> getRequiredTestClass​()
      Get the required Class associated with the current test or container.
      default java.lang.Object getRequiredTestInstance​()
      Get the required test instance associated with the current test or container.
      default java.lang.reflect.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)
      java.util.Set<java.lang.String> getTags​()
      Get the set of all tags for the current test or container.
      java.util.Optional<java.lang.Class<?>> getTestClass​()
      Get the Class associated with the current test or container, if available.
      java.util.Optional<java.lang.Object> getTestInstance​()
      Get the test instance associated with the current test or container, if available.
      java.util.Optional<TestInstance.Lifecycle> getTestInstanceLifecycle​()
      Get the TestInstance.Lifecycle of the test instance associated with the current test or container, if available.
      java.util.Optional<java.lang.reflect.Method> getTestMethod​()
      Get the Method associated with the current test, if available.
      java.lang.String getUniqueId​()
      Get the unique ID of the current test or container.
      default void publishReportEntry​(java.lang.String key, java.lang.String value)
      Publish the specified key-value pair to be consumed by an org.junit.platform.engine.EngineExecutionListener.
      void publishReportEntry​(java.util.Map<java.lang.String,java.lang.String> map)
      Publish a map of key-value pairs to be consumed by an org.junit.platform.engine.EngineExecutionListener.
    • Method Detail

      • getParent

        java.util.Optional<ExtensionContext> getParent​()
        Get the parent extension context, if available.
        Returns:
        an Optional containing the parent; never null but potentially empty
        See Also:
        getRoot()
      • getRoot

        ExtensionContext getRoot​()
        Get the root ExtensionContext.
        Returns:
        the root extension context; never null but potentially this ExtensionContext
        See Also:
        getParent()
      • getUniqueId

        java.lang.String getUniqueId​()
        Get the unique ID of the current test or container.
        Returns:
        the unique ID of the test or container; never null or blank
      • getDisplayName

        java.lang.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 null or blank
      • getTags

        java.util.Set<java.lang.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 null but potentially empty
      • getElement

        java.util.Optional<java.lang.reflect.AnnotatedElement> getElement​()
        Get the 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.

        Returns:
        an Optional containing the AnnotatedElement; never null but potentially empty
        See Also:
        getTestClass(), getTestMethod()
      • getTestClass

        java.util.Optional<java.lang.Class<?>> getTestClass​()
        Get the Class associated with the current test or container, if available.
        Returns:
        an Optional containing the class; never null but potentially empty
        See Also:
        getRequiredTestClass()
      • getRequiredTestClass

        default java.lang.Class<?> getRequiredTestClass​()
        Get the required 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.

        Returns:
        the test class; never null
        Throws:
        PreconditionViolationException - if the test class is not present in this ExtensionContext
      • getTestInstance

        java.util.Optional<java.lang.Object> getTestInstance​()
        Get the test instance associated with the current test or container, if available.
        Returns:
        an Optional containing the test instance; never null but potentially empty
        See Also:
        getRequiredTestInstance()
      • getRequiredTestInstance

        default java.lang.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 this ExtensionContext
      • getTestMethod

        java.util.Optional<java.lang.reflect.Method> getTestMethod​()
        Get the Method associated with the current test, if available.
        Returns:
        an Optional containing the method; never null but potentially empty
        See Also:
        getRequiredTestMethod()
      • getRequiredTestMethod

        default java.lang.reflect.Method getRequiredTestMethod​()
        Get the required 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.

        Returns:
        the test method; never null
        Throws:
        PreconditionViolationException - if the test method is not present in this ExtensionContext
      • getExecutionException

        java.util.Optional<java.lang.Throwable> getExecutionException​()
        Get the exception that was thrown during execution of the test or container associated with this 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.

        Returns:
        an Optional containing the exception thrown; never null but potentially empty if test execution has not (yet) resulted in an exception
      • getConfigurationParameter

        @API(status=STABLE,
             since="5.1")
        java.util.Optional<java.lang.String> getConfigurationParameter​(java.lang.String key)
        Get the configuration parameter stored under the specified key.

        If no such key is present in the ConfigurationParameters for 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; never null or blank
        Returns:
        an Optional containing the value; never null but potentially empty
        Since:
        5.1
        See Also:
        System.getProperty(String), ConfigurationParameters
      • publishReportEntry

        void publishReportEntry​(java.util.Map<java.lang.String,java.lang.String> map)
        Publish a map of key-value pairs to be consumed by an org.junit.platform.engine.EngineExecutionListener.
        Parameters:
        map - the key-value pairs to be published; never null; keys and values within entries in the map also must not be null or blank
      • publishReportEntry

        default void publishReportEntry​(java.lang.String key,
                                        java.lang.String value)
        Publish the specified key-value pair to be consumed by an org.junit.platform.engine.EngineExecutionListener.
        Parameters:
        key - the key of the published pair; never null or blank
        value - the value of the published pair; never null or blank