Close

JUnit 5 - @TestInstance Basics

[Last Updated: Dec 6, 2025]

@TestInstance Annotation in JUnit 5

The @TestInstance annotation in JUnit 5 controls the lifecycle of test instances. It determines whether a new test class instance is created for each test method or if a single instance is shared across all test methods.

This annotation accepts one of two values from the TestInstance.Lifecycle enum:

  • PER_METHOD (default) - New instance for each test method
  • PER_CLASS - Single instance shared across all test methods

Java source and doc

Definition of TestInstance

(Version: junit-5 5.0.0)
package org.junit.jupiter.api;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
@API(status = STABLE, since = "5.0")
public @interface TestInstance {
	enum Lifecycle {
		PER_CLASS, 1
		PER_METHOD; 2
	}
	Lifecycle value(); 3
}
1A new test instance will be created once per test class.
2A new test instance will be created for each test method or factory method. This is analogous to JUnit 1-4 behavior.
3The test instance lifecycle mode to use.

In next tutorials we will see the examples of @TestInstance.

See Also