org.springframework.test.annotation

Annotation Type IfProfileValue



  • @Target(value={TYPE,METHOD})
     @Retention(value=RUNTIME)
     @Documented
     @Inherited
    public @interface IfProfileValue
    Test annotation to indicate whether a test is enabled or disabled for a specific testing profile.

    In the context of this annotation, the term profile refers to a Java system property by default; however, the semantics can be changed by implementing a custom ProfileValueSource. If the configured ProfileValueSource returns a matching value() for the declared name(), the test will be enabled. Otherwise, the test will be disabled and effectively ignored.

    @IfProfileValue can be applied at the class level, the method level, or both. Class-level usage of @IfProfileValue takes precedence over method-level usage for any methods within that class or its subclasses. Specifically, a test is enabled if it is enabled both at the class level and at the method level; the absence of @IfProfileValue means the test is implicitly enabled. This is analogous to the semantics of JUnit's @Ignore annotation, except that the presence of @Ignore always disables a test.

    Example

    When using SystemProfileValueSource as the ProfileValueSource implementation (which is configured by default), you can configure a test method to run only on Java VMs from Oracle as follows:
     @IfProfileValue(name = "java.vendor", value = "Oracle Corporation")
     public void testSomething() {
         // ...
     }

    'OR' Semantics

    You can alternatively configure @IfProfileValue with OR semantics for multiple values(). The following test will be enabled if a ProfileValueSource has been appropriately configured for the "test-groups" profile with a value of either unit-tests or integration-tests. This functionality is similar to TestNG's support for test groups and JUnit's experimental support for test categories.

     @IfProfileValue(name = "test-groups", values = { "unit-tests", "integration-tests" })
     public void testWhichRunsForUnitOrIntegrationTestGroups() {
         // ...
     }

    @IfProfileValue vs. @Profile

    Although the @IfProfileValue and @Profile annotations both involve profiles, they are not directly related. @Profile involves bean definition profiles configured in the Environment; whereas, @IfProfileValue is used to enable or disable tests.

    Meta-annotation Support

    As of Spring Framework 4.0, this annotation may be used as a meta-annotation to create custom composed annotations.

    Since:
    2.0
    Author:
    Rod Johnson, Sam Brannen
    See Also:
    ProfileValueSource, SystemProfileValueSource, ProfileValueSourceConfiguration, ProfileValueUtils, AbstractJUnit4SpringContextTests, SpringJUnit4ClassRunner, ProfileValueChecker, Profile, ActiveProfiles
    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element and Description
      java.lang.String name
      The name of the profile value against which to test.
    • Optional Element Summary

      Optional Elements 
      Modifier and Type Optional Element and Description
      java.lang.String value
      A single, permissible value of the profile value for the given name().
      java.lang.String[] values
      A list of all permissible values of the profile value for the given name().
    • Element Detail

      • name

        public abstract java.lang.String name
        The name of the profile value against which to test.
      • value

        public abstract java.lang.String value
        A single, permissible value of the profile value for the given name().

        Note: Assigning values to both #value and values() will lead to a configuration conflict.

        Default:
        ""
      • values

        public abstract java.lang.String[] values
        A list of all permissible values of the profile value for the given name().

        Note: Assigning values to both value() and #values will lead to a configuration conflict.

        Default:
        {}