Annotation Type TestConstructor
@Target(TYPE) @Retention(RUNTIME) @Documented @Inherited public @interface TestConstructor
@TestConstructoris a type-level annotation that is used to configure how the parameters of a test class constructor are autowired from components in the test'sApplicationContext.If
@TestConstructoris not present or meta-present on a test class, the default test constructor autowire mode will be used. SeeTEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAMEfor details on how to change the default mode. Note, however, that a local declaration of@Autowiredon a constructor takes precedence over both@TestConstructorand 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
SpringExtensionfor use with JUnit Jupiter. Note that theSpringExtensionis often automatically registered for you — for example, when using annotations such as@SpringJUnitConfigand@SpringJUnitWebConfigor various test-related annotations from Spring Boot Test.- Since:
- 5.2
- Author:
- Sam Brannen
- See Also:
@Autowired,SpringExtension,@SpringJUnitConfig,@SpringJUnitWebConfig,@ContextConfiguration,@ContextHierarchy,@ActiveProfiles,@TestPropertySource
Field Summary
Fields Modifier and Type Fields Description static StringTEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAMEJVM system property used to change the default test constructor autowire mode: "spring.test.constructor.autowire.mode".
Required Element Summary
Required Elements Modifier and Type Required Element Description TestConstructor.AutowireModeautowireModeFlag for setting the test constructor autowire mode for the current test class.
Field Detail
TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME
static final String 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.ALLby 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.ANNOTATEDsemantics by default.May alternatively be configured via the
SpringPropertiesmechanism.- See Also:
autowireMode()
Element Detail
autowireMode
TestConstructor.AutowireMode autowireMode
Flag 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_NAMEfor details on how to change the global default.- Returns:
- an
TestConstructor.AutowireModeto take precedence over the global default - See Also:
TEST_CONSTRUCTOR_AUTOWIRE_MODE_PROPERTY_NAME,@Autowired,TestConstructor.AutowireMode.ALL,TestConstructor.AutowireMode.ANNOTATED