Test Overview
This test demonstrates the use of the @BeforeAll method in JUnit. It sets up a shared resource once before all tests run and verifies that the resource is initialized correctly.
This test demonstrates the use of the @BeforeAll method in JUnit. It sets up a shared resource once before all tests run and verifies that the resource is initialized correctly.
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.*; public class BeforeAllExampleTest { static String sharedResource; @BeforeAll static void setup() { sharedResource = "Initialized"; } @Test void testResourceIsInitialized() { assertNotNull(sharedResource, "Shared resource should be initialized"); assertEquals("Initialized", sharedResource, "Shared resource should have correct value"); } @Test void testResourceValue() { assertTrue(sharedResource.startsWith("Init"), "Shared resource should start with 'Init'"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | JUnit test runner starts and calls @BeforeAll static method 'setup' | sharedResource is set to 'Initialized' | - | PASS |
| 2 | JUnit runs test method 'testResourceIsInitialized' | sharedResource is 'Initialized' | assertNotNull(sharedResource) and assertEquals(sharedResource, 'Initialized') | PASS |
| 3 | JUnit runs test method 'testResourceValue' | sharedResource is 'Initialized' | assertTrue(sharedResource.startsWith('Init')) | PASS |