类 TestContextManager
TestContextManager is the main entry point into the Infra
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.
before test class execution: prior to any before class callbacks of a particular testing framework (e.g., JUnit 4's@BeforeClass)test instance preparation: immediately following instantiation of the test classbefore test setup: prior to any before method callbacks of a particular testing framework (e.g., JUnit 4's@Before)before test execution: immediately before execution of the test method but after test setupafter test execution: immediately after execution of the test method but before test tear downafter test tear down: after any after method callbacks of a particular testing framework (e.g., JUnit 4's@After)after test class execution: after any after class callbacks of a particular testing framework (e.g., JUnit 4's@AfterClass)
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.
- 从以下版本开始:
- 4.0
- 作者:
- Sam Brannen, Juergen Hoeller, Harry Yang
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明private static final cn.taketoday.logging.Loggerprivate final TestContextprivate final ThreadLocal<TestContext>private final List<TestExecutionListener> -
构造器概要
构造器构造器说明TestContextManager(TestContextBootstrapper testContextBootstrapper) Construct a newTestContextManagerusing the suppliedTestContextBootstrapperand register the necessaryTestExecutionListeners.TestContextManager(Class<?> testClass) Construct a newTestContextManagerfor the supplied test class. -
方法概要
修饰符和类型方法说明voidHook for post-processing a test class after execution of all tests within the class.voidafterTestExecution(Object testInstance, Method testMethod, Throwable 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.voidafterTestMethod(Object testInstance, Method testMethod, Throwable 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.voidHook for pre-processing a test class before execution of any tests within the class.voidbeforeTestExecution(Object testInstance, Method testMethod) 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.voidbeforeTestMethod(Object testInstance, Method testMethod) 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.private static TestContextcopyTestContext(TestContext testContext) Attempt to create a copy of the suppliedTestContextusing its copy constructor.private List<TestExecutionListener>Get a copy of theTestExecutionListenersregistered for thisTestContextManagerin reverse order.final TestContextGet theTestContextmanaged by thisTestContextManager.final List<TestExecutionListener>Get the currentTestExecutionListenersregistered for thisTestContextManager.private voidhandleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) private static booleanprivate voidlogException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) private voidlogException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) private voidprepareForAfterCallback(String callbackName, Object testInstance, Method testMethod, Throwable exception) private voidprepareForBeforeCallback(String callbackName, Object testInstance, Method testMethod) voidprepareTestInstance(Object testInstance) Hook for preparing a test instance prior to execution of any individual test methods — for example, to inject dependencies.private static voidvoidregisterTestExecutionListeners(TestExecutionListener... testExecutionListeners) Register the supplied array ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.voidregisterTestExecutionListeners(List<TestExecutionListener> testExecutionListeners) Register the supplied list ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager.private static String
-
字段详细资料
-
log
private static final cn.taketoday.logging.Logger log -
skippedExceptionTypes
-
testContext
-
testContextHolder
-
testExecutionListeners
-
-
构造器详细资料
-
TestContextManager
Construct a newTestContextManagerfor the supplied test class.Delegates to
TestContextManager(TestContextBootstrapper)with theTestContextBootstrapperconfigured for the test class. If the@BootstrapWithannotation is present on the test class, either directly or as a meta-annotation, then itsvaluewill be used as the bootstrapper type; otherwise, theDefaultTestContextBootstrapperwill be used.- 参数:
testClass- the test class to be managed- 另请参阅:
-
TestContextManager
Construct a newTestContextManagerusing the suppliedTestContextBootstrapperand register the necessaryTestExecutionListeners.Delegates to the supplied
TestContextBootstrapperfor building theTestContextand retrieving theTestExecutionListeners.- 参数:
testContextBootstrapper- the bootstrapper to use- 另请参阅:
-
-
方法详细资料
-
getTestContext
Get theTestContextmanaged by thisTestContextManager. -
registerTestExecutionListeners
Register the supplied list ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager. -
registerTestExecutionListeners
Register the supplied array ofTestExecutionListenersby appending them to the list of listeners used by thisTestContextManager. -
getTestExecutionListeners
Get the currentTestExecutionListenersregistered for thisTestContextManager.Allows for modifications, e.g. adding a listener to the beginning of the list. However, make sure to keep the list stable while actually executing tests.
-
getReversedTestExecutionListeners
Get a copy of theTestExecutionListenersregistered for thisTestContextManagerin reverse order. -
beforeTestClass
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
TestExecutionListenera chance to pre-process the test class execution. If a listener throws an exception, however, the remaining registered listeners will not be called.- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
prepareTestInstance
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
InfraMethodRule). In any case, this method must be called prior to any framework-specific lifecycle callbacks.The managed
TestContextwill be updated with the suppliedtestInstance.An attempt will be made to give each registered
TestExecutionListenera chance to prepare the test instance. If a listener throws an exception, however, the remaining registered listeners will not be called.- 参数:
testInstance- the test instance to prepare- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
beforeTestMethod
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 namedbeforeTestMethod. Since the introduction ofbeforeTestExecution(java.lang.Object, java.lang.reflect.Method), a more suitable name for this method might be something likebeforeTestSetUporbeforeEach; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.The managed
TestContextwill be updated with the suppliedtestInstanceandtestMethod.An attempt will be made to give each registered
TestExecutionListenera chance to perform its pre-processing. If a listener throws an exception, however, the remaining registered listeners will not be called.- 参数:
testInstance- the current test instancetestMethod- the test method which is about to be executed on the test instance- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
beforeTestExecution
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
TestContextwill be updated with the suppliedtestInstanceandtestMethod.An attempt will be made to give each registered
TestExecutionListenera chance to perform its pre-processing. If a listener throws an exception, however, the remaining registered listeners will not be called.- 参数:
testInstance- the current test instancetestMethod- the test method which is about to be executed on the test instance- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
beforeTestMethod(java.lang.Object, java.lang.reflect.Method)afterTestMethod(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)beforeTestExecution(java.lang.Object, java.lang.reflect.Method)afterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable)getTestExecutionListeners()
-
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
TestContextwill be updated with the suppliedtestInstance,testMethod, andexception.Each registered
TestExecutionListenerwill 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 registered listeners will be executed in the opposite order in which they were registered.
- 参数:
testInstance- the current test instancetestMethod- the test method which has just been executed on the test instanceexception- the exception that was thrown during execution of the test method or by a TestExecutionListener, ornullif none was thrown- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
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 namedafterTestMethod. Since the introduction ofafterTestExecution(java.lang.Object, java.lang.reflect.Method, java.lang.Throwable), a more suitable name for this method might be something likeafterTestTearDownorafterEach; however, it is unfortunately impossible to rename this method due to backward compatibility concerns.The managed
TestContextwill be updated with the suppliedtestInstance,testMethod, andexception.Each registered
TestExecutionListenerwill 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 registered listeners will be executed in the opposite
- 参数:
testInstance- the current test instancetestMethod- the test method which has just been executed on the test instanceexception- the exception that was thrown during execution of the test method or by a TestExecutionListener, ornullif none was thrown- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
afterTestClass
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
TestExecutionListenerwill 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 registered listeners will be executed in the opposite
- 抛出:
Exception- if a registered TestExecutionListener throws an exception- 另请参阅:
-
prepareForBeforeCallback
-
prepareForAfterCallback
-
handleBeforeException
private void handleBeforeException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) throws Exception - 抛出:
Exception
-
logException
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Class<?> testClass) -
logException
private void logException(Throwable ex, String callbackName, TestExecutionListener testExecutionListener, Object testInstance, Method testMethod) -
copyTestContext
Attempt to create a copy of the suppliedTestContextusing its copy constructor. -
typeName
-
registerSkippedExceptionType
-
isSkippedException
-