The following annotations are supported when used in combination with SpringExtension
and JUnit Jupiter (that is, the programming model in JUnit 5):
@SpringJUnitConfig
@SpringJUnitConfig
is a compound
annotation that combines @ExtendWith(SpringExtension.class)
from JUnit Jupiter with @ContextConfiguration
from Spring TestContext Framework. It can be used at the class level as a replacement for @ContextConfiguration
.
Regarding configuration options, the only difference between the @ContextConfiguration
and @SpringJUnitConfig
annotations is that in the @SpringJUnitConfig annotation, component classes can be declared with a
value
attribute.
The following example shows how to use the
@SpringJUnitConfig
annotation to specify a configuration class:
@SpringJUnitConfig(TestConfig.class)
class ConfigurationClassJUnitJupiterSpringTests {
// class body...
}
- Set the configuration class.
@SpringJUnitConfig(TestConfig::class)
class ConfigurationClassJUnitJupiterSpringTests {
// class body...
}
- Set the configuration class.
The following example shows how to use the @SpringJUnitConfig
annotation to set the location of
the configuration file:
@SpringJUnitConfig(locations = "/test-config.xml")
class XmlJUnitJupiterSpringTests {
// class body...
}
- Set the location of the configuration file.
@SpringJUnitConfig(locations = ["/test-config.xml"])
class XmlJUnitJupiterSpringTests {
// class body...
}
- Set the location of the configuration file.
For more information, see "Context
Management", as well as in the javadoc at @SpringJUnitConfig
and @ContextConfiguration
.
@SpringJUnitWebConfig
@SpringJUnitWebConfig
is a compound annotation that combines the @ExtendWith(SpringExtension.class)
annotation from JUnit Jupiter with @ContextConfiguration
and
@WebAppConfiguration
annotations from Spring TestContext Framework. It can be used at the class
level as a replacement for the @ContextConfiguration
and @WebAppConfiguration
annotations. Regarding configuration options, the only difference between the @ContextConfiguration
and @SpringJUnitWebConfig
annotations is that in the @SpringJUnitWebConfig
annotation,
component classes can be declared with using the value
attribute. Additionally, the value attribute
from the @WebAppConfiguration
annotation can only be overridden by using the resourcePath attribute
in the @SpringJUnitWebConfig
annotation.
The following example shows how to use the annotation @SpringJUnitWebConfig
to set the configuration
class:
@SpringJUnitWebConfig(TestConfig.class)
class ConfigurationClassJUnitJupiterSpringWebTests {
// class body...
}
- Set the configuration class.
@SpringJUnitWebConfig(TestConfig::class)
class ConfigurationClassJUnitJupiterSpringWebTests {
// class body...
}
- Set the configuration class.
The following example shows how to use the @SpringJUnitWebConfig
annotation to set the location of
the configuration file:
@SpringJUnitWebConfig(locations = "/test-config.xml")
class XmlJUnitJupiterSpringWebTests {
// class body ...
}
- Set the location of the configuration file.
@SpringJUnitWebConfig(locations = ["/test-config.xml"])
class XmlJUnitJupiterSpringWebTests {
// class body...
}
- Set the location of the configuration file.
For more information, see "Context
Management", as well as in javadoc at @SpringJUnitWebConfig
, @ContextConfiguration
and @WebAppConfiguration
.
@TestConstructor
@TestConstructor
is a type-level annotation that is used to configure how the test class constructor
parameters will be automatically determined and linked from components in the ApplicationContext
of
the test.
If the @TestConstructor
annotation is not present or is present through a meta annotation in the
test class, the default test constructor detection and binding mode will be used. For details on how to
change the default mode, see the tip below. Note, however, that the local declaration of the
@Autowired
annotation for a constructor takes precedence over the @TestConstructor
annotation and default mode.
Test constructor auto-discovery and binding mode the default can be changed by setting the system
property spring.test.constructor.autowire.mode
from the JVM to all
. In addition, the
default mode can be set through the SpringProperties
.
Starting with Spring Framework 5.3, the default mode can also be configured as JUnit Platform configuration parameter.
If the spring.test.constructor.autowire.mode
property is not set, constructors test classes will not
be automatically discovered and associated.
@TestConstructor
annotation is supported only in combination with SpringExtension
for use with JUnit Jupiter. Note that
SpringExtension
is often registered automatically - for example, when using the @SpringJUnitConfig
and @SpringJUnitWebConfig
annotations or various test-related annotations from Spring Boot Test .
@NestedTestConfiguration
@NestedTestConfiguration
is a type-level annotation that is used to configure whether how test
configuration annotations in Spring are handled within the class hierarchy for internal test classes.
If the
@NestedTestConfiguration
annotation is not present or is present through a meta annotation in a test
class, in its hierarchy supertypes or in the hierarchy of its enclosing classes, the default inheritance mode of
the enclosing configuration will be used. For details on how to change the default mode, see the tip below.
The default encompassing configuration inheritance mode is INHERIT
, but it can be changed
by setting the system property spring.test.enclosing.configuration
from JVM to
OVERRIDE
. Alternatively, the default mode can be set via the SpringProperties
.
Spring
TestContext Framework supports semantics of the @NestedTestConfiguration
annotation for the
following annotations.
@NestedTestConfiguration
annotation usually only makes
sense in combination with test classes from JUnit Jupiter marked with the @Nested
annotation; however,
there may be other test frameworks that support Spring and nested test classes that use this annotation.
See: section "Configuration
of a test class with the annotation @Nested
" to view the example and get more detailed
information.
@EnabledIf
Annotation @EnabledIf
is used to
signal that an annotated JUnit Jupiter test class or test method is enabled and should be executed if the specified
expression
is true
. Specifically, if the expression is Boolean.TRUE
or String
is true
(ignored case), the test is fired. When applied at the class level, all test methods within
that class are also automatically enabled by default.
Expressions can take any of the following forms:
Language expression Spring Expression Language (SpEL). Example:
@EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
Placeholder for a property available in
Environment
of the Spring framework. Example:@EnabledIf("${smoke.tests.enabled}")
Text literal. Example:
@EnabledIf("true")
Note, however, that a text literal that is not the result of a property placeholder being dynamically resolved
has zero practical value, since the @EnabledIf("false")
annotation is equivalent to the
@Disabled
annotation, and the @EnabledIf("true")
annotation is logically meaningless .
You can use the
@EnabledIf
annotation as a meta annotation to create special compound annotations. For example, you can
create a custom annotation @EnabledOnMac
as follows:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@EnabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Enabled on Mac OS"
)
public @interface EnabledOnMac {}
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@EnabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Enabled on Mac OS"
)
annotation class EnabledOnMac {}
The @EnabledOnMac annotation
serves solely as an example that
this is possible . If this is your case, take advantage of the built-in support for the
@EnabledOnOs(MAC)
annotation in JUnit Jupiter.
Starting with JUnit 5.7, JUnit Jupiter also has a conditional
annotation called @EnabledIf
. Thus, if you need to use the @EnabledIf
annotation support
in Spring, make sure you import the annotation type from the correct package.
@DisabledIf
The @DisabledIf
annotation is used to signal that an annotated test class
from JUnit Jupiter or test method is disabled and should not be executed unless the provided
expression
turns out to be true
. In particular, if the expression is
Boolean.TRUE
or String
is true
(ignored case), the test is disabled. When
applied at the class level, all test methods in that class are also automatically disabled.
Expressions can take any of the following forms:
Language expression Spring Expression Language (SpEL). Example:
@DisabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
Placeholder for a property available in the
Environment
Spring framework. Example:@DisabledIf("${smoke.tests.disabled}")
Text literal. Example:
@DisabledIf("true")
Note, however, that a text literal that is not the result of a property placeholder being dynamically resolved
has zero practical value, since the @DisabledIf("true")
annotation is equivalent to the
@Disabled
annotation, and the @DisabledIf("false")
annotation is logically meaningless .
You can use the
@DisabledIf
annotation as a meta annotation to create special compound annotations. For example, you
can create a custom annotation @DisabledOnMac
as follows:
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@DisabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Disabled on Mac OS"
)
public @interface DisabledOnMac {}
@Target(AnnotationTarget.TYPE, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@DisabledIf(
expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
reason = "Disabled on Mac OS"
)
annotation class DisabledOnMac {}
The @DisabledOnMac annotation
serves solely as an example
that this is possible . If this is your case, take advantage of the built-in support for the @DisabledOnOs(MAC)
annotation in JUnit Jupiter.
Starting with JUnit 5.7, JUnit Jupiter also has a conditional
annotation called @DisabledIf
. So, if you need to use the @DisabledIf
annotation support
in Spring, make sure you import the annotation type from the correct package.
GO TO FULL VERSION