Test Overview
This test checks that two JUnit test methods run independently without sharing state. It verifies that the counter variable resets for each test method, ensuring test independence.
This test checks that two JUnit test methods run independently without sharing state. It verifies that the counter variable resets for each test method, ensuring test independence.
import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; public class CounterTest { private int counter; @BeforeEach void setUp() { counter = 0; } @Test void testIncrementOnce() { counter++; assertEquals(1, counter); } @Test void testIncrementTwice() { counter++; counter++; assertEquals(2, counter); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit framework initializes test class instance and calls @BeforeEach method | counter variable is set to 0 | - | PASS |
| 2 | testIncrementOnce method runs and increments counter by 1 | counter value is 1 | assertEquals(1, counter) verifies counter is 1 | PASS |
| 3 | JUnit framework initializes new test class instance and calls @BeforeEach method again | counter variable is reset to 0 | - | PASS |
| 4 | testIncrementTwice method runs and increments counter by 2 | counter value is 2 | assertEquals(2, counter) verifies counter is 2 | PASS |