@TestInstance
is a type-level annotation that is used to configure
the lifecycle of test instances for the annotated
test class or test interface.
If @TestInstance
is not explicitly declared on a test class or
on a test interface implemented by a test class, the lifecycle mode will
implicitly default to PER_METHOD
. Note, however,
that an explicit lifecycle mode is inherited within a test class
hierarchy. In addition, the default lifecycle mode may be overridden
via the junit.jupiter.testinstance.lifecycle.default configuration
parameter which can be supplied via the Launcher
API, build tools
(e.g., Gradle and Maven), a JVM system property, or the JUnit Platform
configuration file (i.e., a file named junit-platform.properties
in
the root of the class path). Consult the User Guide for further information.
Setting the test instance lifecycle mode to PER_CLASS
enables the following features.
@BeforeAll
and
@AfterAll
methods in the test class.@BeforeAll
and @AfterAll
methods
in @Nested
test classes. Beginning with Java 16, @BeforeAll
and @AfterAll
methods may be declared as static
in
@Nested
test classes with either lifecycle mode.@BeforeAll
and @AfterAll
on interface
default
methods.@BeforeAll
and @AfterAll
lifecycle methods as well as @MethodSource
factory methods in test classes
implemented with the Kotlin programming language.@TestInstance
may also be used as a meta-annotation in order to
create a custom composed annotation that inherits the semantics
of @TestInstance
.
@Nested
Modifier and Type | Class and Description |
---|---|
public static enum | TestInstance.
Enumeration of test instance lifecycle modes. |
Modifier and Type | Method and Description |
---|---|
public TestInstance. |
value | back to summary |
---|---|
public TestInstance. The test instance lifecycle mode to use. |
PER_METHOD
, PER_CLASS
Modifier and Type | Field and Description |
---|---|
public static final String | DEFAULT_LIFECYCLE_PROPERTY_NAME
Property name used to set the default test instance lifecycle mode: junit.jupiter.testinstance.lifecycle.default Supported Values Supported values include names of enum constants defined in
|
public static final TestInstance. | PER_CLASS
When using this mode, a new test instance will be created once per test class. |
public static final TestInstance. | PER_METHOD
When using this mode, a new test instance will be created for each test method, test factory method, or test template method. |
Access | Constructor and Description |
---|---|
private |
Modifier and Type | Method and Description |
---|---|
public static TestInstance. | |
public static TestInstance. |
DEFAULT_LIFECYCLE_PROPERTY_NAME | back to summary |
---|---|
public static final String DEFAULT_LIFECYCLE_PROPERTY_NAME Property name used to set the default test instance lifecycle mode: junit.jupiter.testinstance.lifecycle.default Supported ValuesSupported values include names of enum constants defined in
If not specified, the default is "per_method" which corresponds to
|
PER_CLASS | back to summary |
---|---|
public static final TestInstance. When using this mode, a new test instance will be created once per test class.
|
PER_METHOD | back to summary |
---|---|
public static final TestInstance. When using this mode, a new test instance will be created for each test method, test factory method, or test template method. This mode is analogous to the behavior found in JUnit versions 1 through 4.
|
Lifecycle | back to summary |
---|---|
private Lifecycle() |
valueOf | back to summary |
---|---|
public static TestInstance. |
values | back to summary |
---|---|
public static TestInstance. |