Test Overview
This test demonstrates how assumeTrue and assumeFalse work in JUnit. It shows that if assumptions fail, the test is skipped, not failed.
This test demonstrates how assumeTrue and assumeFalse work in JUnit. It shows that if assumptions fail, the test is skipped, not failed.
import static org.junit.jupiter.api.Assumptions.*; import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; public class AssumptionTest { @Test void testWithAssumeTrue() { assumeTrue(System.getProperty("os.name").startsWith("Windows")); // This assertion runs only if assumeTrue passes assertEquals(4, 2 + 2); } @Test void testWithAssumeFalse() { assumeFalse(System.getProperty("os.name").startsWith("Windows")); // This assertion runs only if assumeFalse passes assertEquals(5, 2 + 3); } }
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts: testWithAssumeTrue | JUnit test runner ready to run testWithAssumeTrue | - | PASS |
| 2 | assumeTrue checks if OS name starts with 'Windows' | System property os.name is 'Windows 10' | assumeTrue condition is true | PASS |
| 3 | Runs assertion assertEquals(4, 2 + 2) | 2 + 2 equals 4 | 4 equals 4 | PASS |
| 4 | Test ends: testWithAssumeTrue | Test passed | - | PASS |
| 5 | Test starts: testWithAssumeFalse | JUnit test runner ready to run testWithAssumeFalse | - | PASS |
| 6 | assumeFalse checks if OS name starts with 'Windows' | System property os.name is 'Windows 10' | assumeFalse condition is true | PASS |
| 7 | Test skipped because assumeFalse failed | Test marked as skipped, no assertions run | - | PASS |