Annotation Interface TestConstructor
@TestConstructor
is an annotation that can be applied to a test class
to configure how the parameters of a test class constructor are autowired from
components in the test's ApplicationContext
.
If @TestConstructor
is not present or meta-present
on a test class, the default test constructor autowire mode will be
used. See TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
for details on
how to change the default mode. Note, however, that a local declaration of
@Autowired
@jakarta.inject.Inject
, or
@javax.inject.Inject
on a constructor takes
precedence over both @TestConstructor
and the default mode.
This annotation may be used as a meta-annotation to create custom composed annotations.
As of Spring Framework 5.2, this annotation is only supported in conjunction
with the SpringExtension
for use with JUnit Jupiter. Note that the SpringExtension
is
often automatically registered for you — for example, when using annotations such as
@SpringJUnitConfig
and
@SpringJUnitWebConfig
or various test-related annotations from Spring Boot Test.
As of Spring Framework 5.3, this annotation will be inherited from an
enclosing test class by default. See
@NestedTestConfiguration
for details.
- Since:
- 5.2
- Author:
- Sam Brannen
- See Also:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic enum
Defines autowiring modes for parameters in a test constructor. -
Required Element Summary
Modifier and TypeRequired ElementDescriptionFlag for setting the test constructor autowire mode for the current test class. -
Field Summary
Modifier and TypeFieldDescriptionstatic final String
JVM system property used to change the default test constructor autowire mode: "spring.test.constructor.autowire.mode".
-
Field Details
-
TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
JVM system property used to change the default test constructor autowire mode: "spring.test.constructor.autowire.mode".Acceptable values include enum constants defined in
TestConstructor.AutowireMode
, ignoring case. For example, the default may be changed toTestConstructor.AutowireMode.ALL
by supplying the following JVM system property via the command line.-Dspring.test.constructor.autowire.mode=all
If the property is not set to
ALL
, parameters for test class constructors will be autowired according toTestConstructor.AutowireMode.ANNOTATED
semantics by default.May alternatively be configured via the
SpringProperties
mechanism.As of Spring Framework 5.3, this property may also be configured as a JUnit Platform configuration parameter.
- See Also:
-
-
Element Details
-
autowireMode
TestConstructor.AutowireMode autowireModeFlag for setting the test constructor autowire mode for the current test class.Setting this flag overrides the global default. See
TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
for details on how to change the global default.- Returns:
- an
TestConstructor.AutowireMode
to take precedence over the global default - See Also:
-