Test Overview
This test demonstrates how JUnit creates a single test instance for all test methods when using @TestInstance(Lifecycle.PER_CLASS). It verifies that the same object is used across tests by incrementing a counter.
This test demonstrates how JUnit creates a single test instance for all test methods when using @TestInstance(Lifecycle.PER_CLASS). It verifies that the same object is used across tests by incrementing a counter.
import org.junit.jupiter.api.*; import static org.junit.jupiter.api.Assertions.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) public class CounterTest { private int counter = 0; @BeforeAll void setup() { counter = 1; } @Test void testIncrement1() { counter++; assertTrue(counter > 1, "Counter should be greater than 1"); } @Test void testIncrement2() { counter++; assertTrue(counter > 2, "Counter should be greater than 2"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit creates a single test instance of CounterTest for all tests due to @TestInstance(PER_CLASS) | CounterTest instance created with counter = 0 | — | PASS |
| 2 | @BeforeAll method setup() runs and sets counter = 1 | CounterTest instance counter is now 1 | — | PASS |
| 3 | testIncrement1() runs, increments counter to 2 | CounterTest instance counter is 2 | assertTrue(counter > 1) verifies counter is 2 | PASS |
| 4 | testIncrement2() runs, increments counter to 3 | CounterTest instance counter is 3 | assertTrue(counter > 2) verifies counter is 3 | PASS |
@TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation do in a JUnit test class?@TestInstance(TestInstance.Lifecycle.PER_CLASS) annotation tells JUnit to create only one instance of the test class for all test methods.@TestInstance(TestInstance.Lifecycle.PER_CLASS).TestInstance. prefix, @TestInstance(PER_CLASS) is incomplete, and @TestInstance(Lifecycle.PER_METHOD) uses PER_METHOD which is default, not PER_CLASS.@TestInstance(TestInstance.Lifecycle.PER_CLASS):
import org.junit.jupiter.api.*;
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class CounterTest {
private int count = 0;
@Test
void testIncrement() {
count++;
Assertions.assertEquals(1, count);
}
@Test
void testIncrementAgain() {
count++;
Assertions.assertEquals(2, count);
}
}
What will be the result when running these tests?count keeps its value between tests.@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class SampleTest {
private List<String> list = new ArrayList<>();
@BeforeEach
void setup() {
list.clear();
list.add("start");
}
@Test
void testOne() {
list.add("one");
Assertions.assertEquals(2, list.size());
}
@Test
void testTwo() {
list.add("two");
Assertions.assertEquals(2, list.size());
}
}
If the list.clear() call is removed from setup(), what problem will occur?list.clear(), the list keeps accumulating elements from previous tests because the same instance is reused.@TestInstance(TestInstance.Lifecycle.PER_CLASS). Which of the following is the best practice to avoid flaky tests?@BeforeEach ensures each test starts fresh, preventing flaky or dependent tests.@BeforeEach method. -> Option CPass the test and this pattern moves unknown