Interface TestInstanceFactory
-
- All Superinterfaces:
Extension,TestInstantiationAwareExtension
- Functional Interface:
- This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference.
@FunctionalInterface @API(status=STABLE, since="5.7") public interface TestInstanceFactory extends TestInstantiationAwareExtensionTestInstanceFactorydefines the API forExtensionsthat wish to create test instances.Common use cases include creating test instances with special construction requirements or acquiring the test instance from a dependency injection framework.
Extensions that implement
TestInstanceFactorymust be registered at the class level.Warning
Only one
TestInstanceFactoryis allowed to be registered for any given test class. Registering multiple factories for any single test class will result in an exception being thrown for all tests in that class, in any subclass, and in any nested class. Note that anyTestInstanceFactoryregistered in a superclass or enclosing class (i.e., in the case of a@Nestedtest class) is inherited. It is therefore the user's responsibility to ensure that only a singleTestInstanceFactoryis registered for any specific test class.Constructor Requirements
Consult the documentation in
Extensionfor details on constructor requirements.ExtensionContextScopeAs of JUnit Jupiter 5.12, this API participates in the
TestInstantiationAwareExtensioncontract. Implementations of this API may therefore choose to overridegetTestInstantiationExtensionContextScope(ExtensionContext)to require a test-method scopedExtensionContext. SeecreateTestInstance(TestInstanceFactoryContext, ExtensionContext)for further details.
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
TestInstantiationAwareExtension.ExtensionContextScope
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.ObjectcreateTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext)Callback for creating a test instance for the supplied context.-
Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
-
-
-
Method Detail
-
createTestInstance
java.lang.Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException
Callback for creating a test instance for the supplied context.By default, the supplied
ExtensionContextrepresents the test class that's about to be instantiated. Extensions may overrideTestInstantiationAwareExtension.getTestInstantiationExtensionContextScope(org.junit.jupiter.api.extension.ExtensionContext)to returnTEST_METHODin order to change the scope of theExtensionContextto the test method, unless thePER_CLASSlifecycle is used. Changing the scope makes test-specific data available to the implementation of this method and allows keeping state on the test level by using the providedStoreinstance.Note: the
ExtensionContextsupplied to aTestInstanceFactorywill always return an emptyOptionalvalue fromgetTestInstance()since the test instance cannot exist before it has been created by aTestInstanceFactoryor the framework itself.- Parameters:
factoryContext- the context for the test class to be instantiated; nevernullextensionContext- the current extension context; nevernull- Returns:
- the test instance; never
null - Throws:
TestInstantiationException- if an error occurs while creating the test instance
-
-