@Target(value=TYPE) @Retention(value=RUNTIME) @Documented @Inherited public @interface TestConstructor
@TestConstructor is a type-level annotation that is used to configure
whether a test class constructor should be automatically 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 #AUTOWIRE_TEST_CONSTRUCTOR_PROPERTY_NAME for details on how to change
the default mode. Note, however, that a local declaration of
@Autowired 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.
@Autowired,
SpringExtension,
@SpringJUnitConfig,
@SpringJUnitWebConfig,
@ContextConfiguration,
@ContextHierarchy,
@ActiveProfiles,
@TestPropertySource| Modifier and Type | Fields and Description |
|---|---|
static String |
TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME
System property used to configure the default test constructor autowire
mode: "spring.test.constructor.autowire".
|
| Modifier and Type | Required Element and Description |
|---|---|
boolean |
autowire
Flag for setting the test constructor autowire mode for the
current test class.
|
public static final String TEST_CONSTRUCTOR_AUTOWIRE_PROPERTY_NAME
May alternatively be configured via the
SpringProperties mechanism.
If the property is not set, test class constructors will not be automatically autowired.
autowire()public abstract boolean autowire
Setting this flag overrides the global default. See
#AUTOWIRE_TEST_CONSTRUCTOR_PROPERTY_NAME for details on how to
change the global default.
true if all test constructor arguments should be autowired
from the test's ApplicationContext#AUTOWIRE_TEST_CONSTRUCTOR_PROPERTY_NAME,
@Autowired