/** * Containers and tests are enabled if {@code @EnabledIf} is present on the * corresponding test class or test method and the configured expression * evaluates to {@code true}. */ @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { return evaluateAnnotation(EnabledIf.class, EnabledIf::expression, EnabledIf::reason, EnabledIf::loadContext, true, context); }
/** * Containers and tests are disabled if {@code @DisabledIf} is present on the * corresponding test class or test method and the configured expression evaluates * to {@code true}. */ @Override public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) { return evaluateAnnotation(DisabledIf.class, DisabledIf::expression, DisabledIf::reason, DisabledIf::loadContext, false, context); }
@DisabledIf("false") private void neverDisabledWithDefaultReason() { }
@FailingTestCase @SpringJUnitConfig(DatabaseConfig.class) @Transactional static class FailingAfterTransactionTestCase { @Test void testNothing() { } @AfterTransaction void afterTransaction() { fail("always failing afterTransaction()"); } }
@SpringJUnitConfig(Config.class) @Nested @DisabledIf("true") class DisabledIfOnClassTests { @Test void foo() { fail("This test must be disabled"); } @Test @DisabledIf("false") void bar() { fail("This test must be disabled due to class-level condition"); } }
@SpringJUnitConfig(Config.class) @Nested @EnabledIf("false") class EnabledIfOnClassTests { @Test void foo() { fail("This test must be disabled"); } @Test @EnabledIf("true") void bar() { fail("This test must be disabled due to class-level condition"); } }
@SpringJUnitConfig static class TestCase { @BeforeEach void beforeEach(@Autowired ApplicationContext context) { } @RepeatedTest(NUM_TESTS) void repeatedTest(@Autowired ApplicationContext context) { } @AfterEach void afterEach(@Autowired ApplicationContext context) { } @Configuration static class Config { } }
@Test void disabledWithCustomReason() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("customReason")), true, is(equalTo("Because... 42!"))); }
@Bean TestTemplateInvocationContextProvider provider(List<String> parameters) { return new StringInvocationContextProvider(parameters); } }
@FailingTestCase @ExtendWith(SpringExtension.class) private static abstract class BaseTestCase { @Test void testNothing() { } }
@FailingTestCase @SpringJUnitConfig(DatabaseConfig.class) @Transactional static class FailingBeforeTransactionTestCase { @Test void testNothing() { } @BeforeTransaction void beforeTransaction() { fail("always failing beforeTransaction()"); } }
@Test void notDisabledWithDefaultReason() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("neverDisabledWithDefaultReason")), false, endsWith( "neverDisabledWithDefaultReason() is enabled because @DisabledIf(\"false\") did not evaluate to true")); }
@DisabledIf("\t") private void blankExpression() { }
@Test void disabledWithDefaultReason() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("defaultReason")), true, endsWith("defaultReason() is disabled because @DisabledIf(\"#{1 + 1 eq 2}\") evaluated to true")); }
@DisabledIf("#{1 + 1 eq 2}") private void defaultReason() { }
@Test void missingDisabledIf() { assertResult(condition.evaluateExecutionCondition(buildExtensionContext("missingDisabledIf")), false, endsWith("missingDisabledIf() is enabled since @DisabledIf is not present")); }
@DisabledIf("#{'enigma'}") private void stringExpressionThatIsNeitherTrueNorFalse() { }
@DisabledIf(expression = "#{6 * 7 == 42}", reason = "Because... 42!") private void customReason() { }
@DisabledIf("") private void emptyExpression() { }
@DisabledIf("#{6 * 7}") private void nonBooleanOrStringExpression() { }