@TestInstance Lifecycle Comparison
This tutorial demonstrates the practical differences between PER_METHOD and PER_CLASS lifecycles.
Key Comparison Points
- PER_METHOD: New instance per test method, high isolation
- PER_CLASS: Single instance shared, maintains state
Side-by-Side Example
PER_METHOD Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestInstance;
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
public class PerMethodExampleTest {
private String instanceId = this.toString();
private int operationCount = 0;
@Test
void testDatabaseConnection() {
operationCount++;
System.out.println("PER_METHOD - TestDatabaseConnection");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
@Test
void testDataInsert() {
operationCount++;
System.out.println("PER_METHOD - TestDataInsert");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
@Test
void testDataQuery() {
operationCount++;
System.out.println("PER_METHOD - TestDataQuery");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
}
PER_CLASS Example
package com.logicbig.example;
import org.junit.jupiter.api.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class PerClassExampleTest {
private String instanceId = this.toString();
private int operationCount = 0;
private StringBuilder auditLog = new StringBuilder();
@BeforeAll
void setupSharedResources() {
System.out.println("PER_CLASS - SetupSharedResources");
System.out.println(" Shared Instance ID: " + instanceId);
auditLog.append("Setup completed\n");
}
@Test
void testDatabaseConnection() {
operationCount++;
auditLog.append("Database connected - Operation: " + operationCount + "\n");
System.out.println("PER_CLASS - TestDatabaseConnection");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
@Test
void testDataInsert() {
operationCount++;
auditLog.append("Data inserted - Operation: " + operationCount + "\n");
System.out.println("PER_CLASS - TestDataInsert");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
@Test
void testDataQuery() {
operationCount++;
auditLog.append("Data queried - Operation: " + operationCount + "\n");
System.out.println("PER_CLASS - TestDataQuery");
System.out.println(" Instance ID: " + instanceId);
System.out.println(" Operation Count: " + operationCount);
}
@AfterAll
void cleanupAndReport() {
System.out.println("PER_CLASS - CleanupAndReport");
System.out.println(" Final Operation Count: " + operationCount);
System.out.println(" Audit Log:\n" + auditLog.toString());
}
}
Running Both Example
mvn test OutputD:\example-projects\junit-5\test-instance\junit-5-test-instance-lifecycle>mvn test [INFO] Scanning for projects... [INFO] [INFO] --------< com.logicbig.example:junit-5-test-instance-lifecycle >-------- [INFO] Building junit-5-test-instance-lifecycle 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-test-instance-lifecycle --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\test-instance\junit-5-test-instance-lifecycle\src\main\resources [INFO] [INFO] --- compiler:3.11.0:compile (default-compile) @ junit-5-test-instance-lifecycle --- [INFO] No sources to compile [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-test-instance-lifecycle --- [WARNING] Using platform encoding (Cp1252 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\test-instance\junit-5-test-instance-lifecycle\src\test\resources [INFO] [INFO] --- compiler:3.11.0:testCompile (default-testCompile) @ junit-5-test-instance-lifecycle --- [INFO] Nothing to compile - all classes are up to date [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-test-instance-lifecycle --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.PerClassExampleTest PER_CLASS - SetupSharedResources Shared Instance ID: com.logicbig.example.PerClassExampleTest@6f96c77 PER_CLASS - TestDatabaseConnection Instance ID: com.logicbig.example.PerClassExampleTest@6f96c77 Operation Count: 1 PER_CLASS - TestDataInsert Instance ID: com.logicbig.example.PerClassExampleTest@6f96c77 Operation Count: 2 PER_CLASS - TestDataQuery Instance ID: com.logicbig.example.PerClassExampleTest@6f96c77 Operation Count: 3 PER_CLASS - CleanupAndReport Final Operation Count: 3 Audit Log: Setup completed Database connected - Operation: 1 Data inserted - Operation: 2 Data queried - Operation: 3
[INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.092 s -- in com.logicbig.example.PerClassExampleTest [INFO] Running com.logicbig.example.PerMethodExampleTest PER_METHOD - TestDatabaseConnection Instance ID: com.logicbig.example.PerMethodExampleTest@399f45b1 Operation Count: 1 PER_METHOD - TestDataInsert Instance ID: com.logicbig.example.PerMethodExampleTest@478190fc Operation Count: 1 PER_METHOD - TestDataQuery Instance ID: com.logicbig.example.PerMethodExampleTest@79e2c065 Operation Count: 1 [INFO] Tests run: 3, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.012 s -- in com.logicbig.example.PerMethodExampleTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 6, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.904 s [INFO] Finished at: 2025-12-05T14:20:39+08:00 [INFO] ------------------------------------------------------------------------
As seen above, PER_METHOD, instance variables are reinitialized for every test, whereas with PER_CLASS Instance variables retain their values across different test methods showing they are created and initialized only once.
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|