Test Overview
This test demonstrates how assumptions in JUnit control whether a test runs or is skipped. It verifies that the test only executes when a specific condition is true, otherwise it is skipped.
This test demonstrates how assumptions in JUnit control whether a test runs or is skipped. It verifies that the test only executes when a specific condition is true, otherwise it is skipped.
import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assertions.assertEquals; import org.junit.jupiter.api.Test; public class AssumptionTest { @Test void testOnlyRunsIfConditionTrue() { boolean isFeatureEnabled = false; // Change to true to run test assumeTrue(isFeatureEnabled, "Feature is disabled, skipping test"); // This assertion runs only if assumption passes assertEquals(5, 2 + 3, "Sum should be 5"); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | JUnit test runner initializes the test class | - | PASS |
| 2 | Runs testOnlyRunsIfConditionTrue method | Inside test method, isFeatureEnabled is false | - | PASS |
| 3 | assumeTrue checks if isFeatureEnabled is true | Condition is false, assumption fails | Assumption condition is false | SKIPPED |
| 4 | Test is skipped, assertion is not executed | Test framework marks test as skipped | No assertion executed | PASS (SKIPPED) |