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) |
Assumptions.assumeTrue() in a JUnit test?Assumptions.assumeTrue(condition); with capital A and method assumeTrue.Assumptions.assumeTrue(System.getProperty("os.name").startsWith("Windows"));
assertEquals(4, 2 + 2);System.getProperty("os.name") does not start with "Windows", so assumeTrue condition is false.@Test
void testFeature() {
Assumptions.assumeTrue(isDatabaseAvailable());
assertTrue(runQuery());
}
boolean isDatabaseAvailable() {
return false;
}Assumptions.assumeTrue is not imported or used incorrectly, assumption may not skip test properly.TEST_ENV equals "prod". Which JUnit 5 code snippet correctly uses assumptions to control test execution?Assumptions.assumeTrue(condition) skips the test if condition is false, so test runs only when TEST_ENV equals "prod".Pass the test and this pattern moves unknown