Class TestContextManager

java.lang.Object
org.springframework.test.context.TestContextManager

public class TestContextManager extends Object
TestContextManager is the main entry point into the Spring TestContext Framework.

Specifically, a TestContextManager is responsible for managing a single TestContext and signaling events to each registered TestExecutionListener at the following test execution points.

Support for loading and accessing application contexts, dependency injection of test instances, transactional execution of test methods, etc. is provided by ContextLoaders and TestExecutionListeners, which are configured via @ContextConfiguration and @TestExecutionListeners, respectively.

Bootstrapping of the TestContext, the default ContextLoader, default TestExecutionListeners, and their collaborators is performed by a TestContextBootstrapper, which is configured via @BootstrapWith.

Since:
2.5
Author:
Sam Brannen, Juergen Hoeller
See Also:
  • Constructor Details

  • Method Details

    • getTestContext

      public final TestContext getTestContext()
      Get the TestContext managed by this TestContextManager.
    • registerTestExecutionListeners

      public void registerTestExecutionListeners(List<TestExecutionListener> testExecutionListeners)
      Register the supplied list of TestExecutionListeners by appending them to the list of listeners used by this TestContextManager.
      See Also:
    • registerTestExecutionListeners

      public void registerTestExecutionListeners(TestExecutionListener... testExecutionListeners)
      Register the supplied array of TestExecutionListeners by appending them to the list of listeners used by this TestContextManager.
    • getTestExecutionListeners

      public final List<TestExecutionListener> getTestExecutionListeners()
      Get the current TestExecutionListeners registered for this TestContextManager.

      Allows for modifications, for example, adding a listener to the beginning of the list. However, make sure to keep the list stable while actually executing tests.

    • beforeTestClass

      public void beforeTestClass() throws Exception
      Hook for pre-processing a test class before execution of any tests within the class. Should be called prior to any framework-specific before class methods — for example, methods annotated with JUnit Jupiter's @BeforeAll.

      An attempt will be made to give each registered TestExecutionListener a chance to pre-process the test class execution. If a listener throws an exception, however, the remaining registered listeners will not be called.

      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      Since:
      3.0
      See Also:
    • prepareTestInstance

      public void prepareTestInstance(Object testInstance) throws Exception
      Hook for preparing a test instance prior to execution of any individual test methods — for example, to inject dependencies.

      This method should be called immediately after instantiation of the test class or as soon after instantiation as possible (as is the case with the SpringMethodRule). In any case, this method must be called prior to any framework-specific lifecycle callbacks.

      The managed TestContext will be updated with the supplied testInstance.

      An attempt will be made to give each registered TestExecutionListener a chance to prepare the test instance. If a listener throws an exception, however, the remaining registered listeners will not be called.

      Parameters:
      testInstance - the test instance to prepare
      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      See Also:
    • beforeTestMethod

      public void beforeTestMethod(Object testInstance, Method testMethod) throws Exception
      Hook for pre-processing a test before execution of before lifecycle callbacks of the underlying test framework — for example, setting up test fixtures, starting a transaction, etc.

      This method must be called immediately prior to framework-specific before lifecycle callbacks — for example, methods annotated with JUnit Jupiter's @BeforeEach. For historical reasons, this method is named beforeTestMethod. Since the introduction of beforeTestExecution(Object, Method), a more suitable name for this method might be something like beforeTestSetUp or beforeEach; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.

      The managed TestContext will be updated with the supplied testInstance and testMethod.

      An attempt will be made to give each registered TestExecutionListener a chance to perform its pre-processing. If a listener throws an exception, however, the remaining registered listeners will not be called.

      Parameters:
      testInstance - the current test instance
      testMethod - the test method which is about to be executed on the test instance
      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      See Also:
    • beforeTestExecution

      public void beforeTestExecution(Object testInstance, Method testMethod) throws Exception
      Hook for pre-processing a test immediately before execution of the test method in the supplied test context — for example, for timing or logging purposes.

      This method must be called after framework-specific before lifecycle callbacks — for example, methods annotated with JUnit Jupiter's @BeforeEach.

      The managed TestContext will be updated with the supplied testInstance and testMethod.

      An attempt will be made to give each registered TestExecutionListener a chance to perform its pre-processing. If a listener throws an exception, however, the remaining registered listeners will not be called.

      Parameters:
      testInstance - the current test instance
      testMethod - the test method which is about to be executed on the test instance
      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      Since:
      5.0
      See Also:
    • afterTestExecution

      public void afterTestExecution(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception
      Hook for post-processing a test immediately after execution of the test method in the supplied test context — for example, for timing or logging purposes.

      This method must be called before framework-specific after lifecycle callbacks — for example, methods annotated with JUnit Jupiter's @AfterEach.

      The managed TestContext will be updated with the supplied testInstance, testMethod, and exception.

      Each registered TestExecutionListener will be given a chance to perform its post-processing. If a listener throws an exception, the remaining registered listeners will still be called. After all listeners have executed, the first caught exception will be rethrown with any subsequent exceptions suppressed in the first exception.

      Note that listeners will be executed in the opposite order in which they were registered.

      Parameters:
      testInstance - the current test instance
      testMethod - the test method which has just been executed on the test instance
      exception - the exception that was thrown during execution of the test method or by a TestExecutionListener, or null if none was thrown
      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      Since:
      5.0
      See Also:
    • afterTestMethod

      public void afterTestMethod(Object testInstance, Method testMethod, @Nullable Throwable exception) throws Exception
      Hook for post-processing a test after execution of after lifecycle callbacks of the underlying test framework — for example, tearing down test fixtures, ending a transaction, etc.

      This method must be called immediately after framework-specific after lifecycle callbacks — for example, methods annotated with JUnit Jupiter's @AfterEach. For historical reasons, this method is named afterTestMethod. Since the introduction of afterTestExecution(Object, Method, Throwable), a more suitable name for this method might be something like afterTestTearDown or afterEach; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.

      The managed TestContext will be updated with the supplied testInstance, testMethod, and exception.

      Each registered TestExecutionListener will be given a chance to perform its post-processing. If a listener throws an exception, the remaining registered listeners will still be called. After all listeners have executed, the first caught exception will be rethrown with any subsequent exceptions suppressed in the first exception.

      Note that listeners will be executed in the opposite order in which they were registered.

      Parameters:
      testInstance - the current test instance
      testMethod - the test method which has just been executed on the test instance
      exception - the exception that was thrown during execution of the test method or by a TestExecutionListener, or null if none was thrown
      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      See Also:
    • afterTestClass

      public void afterTestClass() throws Exception
      Hook for post-processing a test class after execution of all tests within the class. Should be called after any framework-specific after class methods — for example, methods annotated with JUnit Jupiter's @AfterAll.

      Each registered TestExecutionListener will be given a chance to perform its post-processing. If a listener throws an exception, the remaining registered listeners will still be called. After all listeners have executed, the first caught exception will be rethrown with any subsequent exceptions suppressed in the first exception.

      Note that listeners will be executed in the opposite order in which they were registered.

      As of Spring Framework 7.0, this method also ensures that the application context for the current TestContext is marked as unused.

      Throws:
      Exception - if a registered TestExecutionListener throws an exception
      Since:
      3.0
      See Also: