Annotation Interface EnabledIf


@Target({TYPE,METHOD}) @Retention(RUNTIME) @Documented @ExtendWith(EnabledIfCondition.class) public @interface EnabledIf
@EnabledIf is used to signal that the annotated test class or test method is enabled and should be executed if the supplied expression() evaluates to true.

When applied at the class level, all test methods within that class are automatically enabled by default as well.

For basic examples, see the Javadoc for expression().

This annotation may be used as a meta-annotation to create custom composed annotations. For example, a custom @EnabledOnMac annotation can be created as follows.

 @Target({ElementType.TYPE, ElementType.METHOD})
 @Retention(RetentionPolicy.RUNTIME)
 @EnabledIf(
     expression = "#{systemProperties['os.name'].toLowerCase().contains('mac')}",
     reason = "Enabled on Mac OS"
 )
 public @interface EnabledOnMac {}
 

Please note that @EnabledOnMac is meant only as an example of what is possible. If you have that exact use case, please use the built-in @EnabledOnOs(MAC) support in JUnit Jupiter.

Since JUnit 5.7, JUnit Jupiter also has a condition annotation named @EnabledIf. Thus, if you wish to use Framework's @EnabledIf support make sure you import the annotation type from the correct package.

从以下版本开始:
4.0
作者:
Sam Brannen
另请参阅:
  • 可选元素概要

    可选元素
    修饰符和类型
    可选元素
    说明
    The expression that will be evaluated to determine if the annotated test class or test method is enabled.
    boolean
    Whether the ApplicationContext associated with the current test should be eagerly loaded in order to evaluate the expression().
    The reason this test is enabled.
    Alias for expression(); only intended to be used if reason() and loadContext() are not specified.
  • 元素详细资料

    • value

      @AliasFor("expression") String value
      Alias for expression(); only intended to be used if reason() and loadContext() are not specified.
      另请参阅:
      默认值:
      ""
    • expression

      @AliasFor("value") String expression
      The expression that will be evaluated to determine if the annotated test class or test method is enabled.

      If the expression evaluates to Boolean.TRUE or a String equal to "true" (ignoring case), the test will be enabled.

      Expressions can be any of the following.

      • Spring Expression Language (SpEL) expression — for example:
        @EnabledIf("#{systemProperties['os.name'].toLowerCase().contains('mac')}")
      • Placeholder for a property available in the Infra Environment — for example:
        @EnabledIf("${smoke.tests.enabled}")
      • Text literal — for example:
        @EnabledIf("true")

      Note, however, that a text literal which is not the result of dynamic resolution of a property placeholder is of zero practical value since @EnabledIf("false") is equivalent to @Disabled and @EnabledIf("true") is logically meaningless.

      另请参阅:
      默认值:
      ""
    • reason

      String reason
      The reason this test is enabled.
      另请参阅:
      默认值:
      ""
    • loadContext

      boolean loadContext
      Whether the ApplicationContext associated with the current test should be eagerly loaded in order to evaluate the expression().

      Defaults to false so that test application contexts are not eagerly loaded unnecessarily. If an expression is based solely on system properties or environment variables or does not interact with beans in the test's application context, there is no need to load the context prematurely since doing so would be a waste of time if the test ends up being disabled.

      另请参阅:
      默认值:
      false