Top Description Methods
org.junit.jupiter.api.extension

public Interface TestInstanceFactory

extends Extension
Annotations
@FunctionalInterface
@API
status:STABLE
since:5.7
Static Imports
org.apiguardian.api.API.Status.STABLE

TestInstanceFactory defines the API for Extensions that 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 TestInstanceFactory must be registered at the class level.

Warning

Only one TestInstanceFactory is 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 any TestInstanceFactory registered in a superclass or enclosing class (i.e., in the case of a @Nested test class) is inherited. It is therefore the user's responsibility to ensure that only a single TestInstanceFactory is registered for any specific test class.

Constructor Requirements

Consult the documentation in Extension for details on constructor requirements.

Since
5.3
See Also
createTestInstance(TestInstanceFactoryContext, ExtensionContext), TestInstanceFactoryContext, TestInstancePostProcessor, TestInstancePreDestroyCallback, ParameterResolver

Method Summary

Modifier and TypeMethod and Description
public Object

Returns:

the test instance; never null
createTestInstance
(TestInstanceFactoryContext
the context for the test class to be instantiated; never null
factoryContext
,
ExtensionContext
the current extension context; never null
extensionContext
)

Callback for creating a test instance for the supplied context.

Method Detail

createTestInstanceback to summary
public Object createTestInstance(TestInstanceFactoryContext factoryContext, ExtensionContext extensionContext) throws TestInstantiationException

Callback for creating a test instance for the supplied context.

Note

the ExtensionContext supplied to a TestInstanceFactory will always return an empty java.util.Optional value from getTestInstance() since the test instance cannot exist before it has been created by a TestInstanceFactory or the framework itself.

Parameters
factoryContext:TestInstanceFactoryContext

the context for the test class to be instantiated; never null

extensionContext:ExtensionContext

the current extension context; never null

Returns:Object

the test instance; never null

Exceptions
TestInstantiationException:
if an error occurs while creating the test instance