接口 TestContext

所有超级接口:
cn.taketoday.core.AttributeAccessor, Serializable
所有已知实现类:
DefaultTestContext

public interface TestContext extends cn.taketoday.core.AttributeAccessor, Serializable
TestContext encapsulates the context in which a test is executed, agnostic of the actual testing framework in use.

concrete implementations are highly encouraged to implement a copy constructor in order to allow the immutable state and attributes of a TestContext to be used as a template for additional contexts created for parallel test execution. The copy constructor must accept a single argument of the type of the concrete implementation. Any implementation that does not provide a copy constructor will likely fail in an environment that executes tests concurrently.

从以下版本开始:
4.0
作者:
Sam Brannen
另请参阅:
  • 方法概要

    修饰符和类型
    方法
    说明
    cn.taketoday.context.ApplicationContext
    Get the application context for this test context, possibly cached.
    Get the test class for this test context.
    Get the exception that was thrown during execution of the test method.
    Get the current test instance for this test context.
    Get the current test method for this test context.
    default boolean
    Determine if the application context for this test context is known to be available.
    void
    Call this method to signal that the application context associated with this test context is dirty and should be removed from the context cache.
    default void
    publishEvent(Function<TestContext,? extends cn.taketoday.context.ApplicationEvent> eventFactory)
    Publish the ApplicationEvent created by the given eventFactory to the application context for this test context.
    void
    updateState(Object testInstance, Method testMethod, Throwable testException)
    Update this test context to reflect the state of the currently executing test.

    从接口继承的方法 cn.taketoday.core.AttributeAccessor

    attributeNames, clearAttributes, computeAttribute, copyAttributesFrom, getAttribute, getAttributeNames, getAttributes, hasAttribute, hasAttributes, removeAttribute, setAttribute
  • 方法详细资料

    • hasApplicationContext

      default boolean hasApplicationContext()
      Determine if the application context for this test context is known to be available.

      If this method returns true, a subsequent invocation of getApplicationContext() should succeed.

      The default implementation of this method always returns false. Custom TestContext implementations are therefore highly encouraged to override this method with a more meaningful implementation. Note that the standard TestContext implementation in Infra overrides this method appropriately.

      返回:
      true if the application context has already been loaded
      另请参阅:
    • getApplicationContext

      cn.taketoday.context.ApplicationContext getApplicationContext()
      Get the application context for this test context, possibly cached.

      Implementations of this method are responsible for loading the application context if the corresponding context has not already been loaded, potentially caching the context as well.

      返回:
      the application context (never null)
      抛出:
      IllegalStateException - if an error occurs while retrieving the application context
      另请参阅:
    • publishEvent

      default void publishEvent(Function<TestContext,? extends cn.taketoday.context.ApplicationEvent> eventFactory)
      Publish the ApplicationEvent created by the given eventFactory to the application context for this test context.

      The ApplicationEvent will only be published if the application context for this test context is available.

      参数:
      eventFactory - factory for lazy creation of the ApplicationEvent
      另请参阅:
    • getTestClass

      Class<?> getTestClass()
      Get the test class for this test context.
      返回:
      the test class (never null)
    • getTestInstance

      Object getTestInstance()
      Get the current test instance for this test context.

      Note: this is a mutable property.

      返回:
      the current test instance (never null)
      另请参阅:
    • getTestMethod

      Method getTestMethod()
      Get the current test method for this test context.

      Note: this is a mutable property.

      返回:
      the current test method (never null)
      另请参阅:
    • getTestException

      @Nullable Throwable getTestException()
      Get the exception that was thrown during execution of the test method.

      Note: this is a mutable property.

      返回:
      the exception that was thrown, or null if no exception was thrown
      另请参阅:
    • markApplicationContextDirty

      void markApplicationContextDirty(@Nullable DirtiesContext.HierarchyMode hierarchyMode)
      Call this method to signal that the application context associated with this test context is dirty and should be removed from the context cache.

      Do this if a test has modified the context — for example, by modifying the state of a singleton bean, modifying the state of an embedded database, etc.

      参数:
      hierarchyMode - the context cache clearing mode to be applied if the context is part of a hierarchy (may be null)
    • updateState

      void updateState(@Nullable Object testInstance, @Nullable Method testMethod, @Nullable Throwable testException)
      Update this test context to reflect the state of the currently executing test.

      WARNING: This method should only be invoked by the TestContextManager.

      Caution: concurrent invocations of this method might not be thread-safe, depending on the underlying implementation.

      参数:
      testInstance - the current test instance (may be null)
      testMethod - the current test method (may be null)
      testException - the exception that was thrown in the test method, or null if no exception was thrown