Top Description Inners Methods
org.junit.jupiter.api

public @Interface TestInstance

extends Annotation
Annotations
@Target:TYPE
@Retention:RUNTIME
@Inherited
@Documented
@API
status:STABLE
since:5.0
Static Imports
org.apiguardian.api.API.Status.STABLE

@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.

Use Cases

Setting the test instance lifecycle mode to PER_CLASS enables the following features.

@TestInstance may also be used as a meta-annotation in order to create a custom composed annotation that inherits the semantics of @TestInstance.

Since
5.0
See Also
@Nested

Nested and Inner Type Summary

Modifier and TypeClass and Description
public static enum
TestInstance.Lifecycle

Enumeration of test instance lifecycle modes.

Method Summary

Modifier and TypeMethod and Description
public TestInstance.Lifecycle
value()

The test instance lifecycle mode to use.

Inherited from java.lang.annotation.Annotation:
annotationTypeequalshashCodetoString

Method Detail

valueback to summary
public TestInstance.Lifecycle value()

The test instance lifecycle mode to use.

org.junit.jupiter.api back to summary

public final Enum TestInstance.Lifecycle

extends Enum<TestInstance.Lifecycle>
Class Inheritance

Enumeration of test instance lifecycle modes.
See Also
PER_METHOD, PER_CLASS

Field Summary

Modifier and TypeField 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 org.junit.jupiter.api.TestInstance.Lifecycle, ignoring case.

public static final TestInstance.Lifecycle
PER_CLASS

When using this mode, a new test instance will be created once per test class.

public static final TestInstance.Lifecycle
PER_METHOD

When using this mode, a new test instance will be created for each test method, test factory method, or test template method.

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static TestInstance.Lifecycle
public static TestInstance.Lifecycle[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinaltoStringvalueOf

Field Detail

DEFAULT_LIFECYCLE_PROPERTY_NAMEback 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 Values

Supported values include names of enum constants defined in org.junit.jupiter.api.TestInstance.Lifecycle, ignoring case.

If not specified, the default is "per_method" which corresponds to @TestInstance(Lifecycle.PER_METHOD).

Annotations
@API
status:STABLE
since:5.9
Since
5.0
See Also
org.junit.jupiter.api.TestInstance
PER_CLASSback to summary
public static final TestInstance.Lifecycle PER_CLASS

When using this mode, a new test instance will be created once per test class.

See Also
PER_METHOD
PER_METHODback to summary
public static final TestInstance.Lifecycle PER_METHOD

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.

See Also
PER_CLASS

Constructor Detail

Lifecycleback to summary
private Lifecycle()

Method Detail

valueOfback to summary
public static TestInstance.Lifecycle valueOf(String name)
valuesback to summary
public static TestInstance.Lifecycle[] values()