Uploaded image for project: 'Spring Framework'
  1. Spring Framework
  2. SPR-11038

Support meta-annotation attribute overrides in the TestContext framework

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Major
    • Resolution: Complete
    • Affects Version/s: 4.0 RC1
    • Fix Version/s: 4.0 RC2
    • Component/s: Test
    • Labels:
      None
    • Last commented by a User:
      false

      Description

      Overview

      SPR-7827 is a nice start, but it didn't go far enough for me (I think).

      Spring now supports overrides of "parent" annotation attributes (through its meta data representation utilities introduced in conjunction with SPR-10181). The Spring TestContext Framework, however, is not yet taking advantage of this feature. So for instance you can annotate a custom annotation with @ContextConfiguration, but there's no way for the user of that annotation to provide their own context configuration locations (e.g. classes=...).

      Deliverables

      Support meta-annotation attribute overrides for the following annotations within the context of the TestContext framework.

      1. @ContextConfiguration
      2. @WebAppConfiguration
      3. @ContextHierarchy
      4. @ActiveProfiles
      5. @DirtiesContext
      6. @TestExecutionListeners
      7. @IfProfileValue
      8. @ProfileValueSourceConfiguration
      9. @Transactional
      10. @BeforeTransaction
      11. @AfterTransaction
      12. @TransactionConfiguration
      13. @Rollback
      14. @Repeat
      15. @Timed
      Notes
      • AnnotatedElementUtils does not support overrides for the 'value' attribute of an annotation. It is therefore not possible or not feasible to support meta-annotation attribute overrides for some test-related annotations.
      • Support for meta-annotation attribute overrides for @Transactional was already supported via SpringTransactionAnnotationParser.
      • @BeforeTransaction and @AfterTransaction do not have attributes.
      • @ContextHierarchy, @WebAppConfiguration, @Rollback, @Repeat, and @ProfileValueSourceConfiguration only have single 'value' attributes which cannot be overridden via Spring's meta-annotation attribute support.
      • Although @IfProfileValue has 'values' and 'name' attributes, the typical usage scenario involves the 'value' attribute which is not supported for meta-annotation attribute overrides. Furthermore, 'name' and 'values' are so generic that it is deemed unfeasible to provide meta-annotation attribute override support for these.

        Issue Links

          Activity

          Hide
          sbrannen Sam Brannen added a comment -

          This has been addressed as described in the comments for GitHub commit c7c395d:

          Support meta-annotation attr overrides in the TCF

          Prior to this commit, the Spring TestContext Framework (TCF) supported
          the use of test-related annotations as meta-annotations for composing
          custom test stereotype annotations; however, attributes in custom
          stereotypes could not be used to override meta-annotation attributes.

          This commit addresses this by allowing attributes from the following
          annotations (when used as meta-annotations) to be overridden in custom
          stereotypes.

          • @ContextConfiguration
          • @ActiveProfiles
          • @DirtiesContext
          • @TransactionConfiguration
          • @Timed
          • @TestExecutionListeners

          This support depends on functionality provided by
          AnnotatedElementUtils. See the 'Notes' below for further details and
          ramifications.

          Notes:

          • AnnotatedElementUtils does not support overrides for the 'value'
            attribute of an annotation. It is therefore not possible or not
            feasible to support meta-annotation attribute overrides for some
            test-related annotations.
          • @ContextHierarchy, @WebAppConfiguration, @Rollback, @Repeat, and
            @ProfileValueSourceConfiguration define single 'value' attributes
            which cannot be overridden via Spring's meta-annotation attribute
            support.
          • Although @IfProfileValue has 'values' and 'name' attributes, the
            typical usage scenario involves the 'value' attribute which is not
            supported for meta-annotation attribute overrides. Furthermore,
            'name' and 'values' are so generic that it is deemed unfeasible to
            provide meta-annotation attribute override support for these.
          • @BeforeTransaction and @AfterTransaction do not define any attributes
            that can be overridden.
          • Support for meta-annotation attribute overrides for @Transactional is
            provided indirectly via SpringTransactionAnnotationParser.

          Implementation Details:

          • MetaAnnotationUtils.AnnotationDescriptor now provides access to the
            AnnotationAttributes for the described annotation.
          • MetaAnnotationUtils.AnnotationDescriptor now provides access to the
            root declaring class as well as the declaring class.
          • ContextLoaderUtils now retrieves AnnotationAttributes from
            AnnotationDescriptor to look up annotation attributes for
            @ContextConfiguration and @ActiveProfiles.
          • ContextConfigurationAttributes now provides a constructor to have its
            attributes sourced from an instance of AnnotationAttributes.
          • ContextLoaderUtils.resolveContextHierarchyAttributes() now throws an
            IllegalStateException if no class in the class hierarchy declares
            @ContextHierarchy.
          • TransactionalTestExecutionListener now uses AnnotatedElementUtils to
            look up annotation attributes for @TransactionConfiguration.
          • Implemented missing unit tests for @Rollback resolution in
            TransactionalTestExecutionListener.
          • SpringJUnit4ClassRunner now uses AnnotatedElementUtils to look up
            annotation attributes for @Timed.
          • TestContextManager now retrieves AnnotationAttributes from
            AnnotationDescriptor to look up annotation attributes for
            @TestExecutionListeners.
          • DirtiesContextTestExecutionListener now uses AnnotatedElementUtils to
            look up annotation attributes for @DirtiesContext.
          Show
          sbrannen Sam Brannen added a comment - This has been addressed as described in the comments for GitHub commit c7c395d : Support meta-annotation attr overrides in the TCF Prior to this commit, the Spring TestContext Framework (TCF) supported the use of test-related annotations as meta-annotations for composing custom test stereotype annotations; however, attributes in custom stereotypes could not be used to override meta-annotation attributes. This commit addresses this by allowing attributes from the following annotations (when used as meta-annotations) to be overridden in custom stereotypes. @ContextConfiguration @ActiveProfiles @DirtiesContext @TransactionConfiguration @Timed @TestExecutionListeners This support depends on functionality provided by AnnotatedElementUtils. See the 'Notes' below for further details and ramifications. Notes: AnnotatedElementUtils does not support overrides for the 'value' attribute of an annotation. It is therefore not possible or not feasible to support meta-annotation attribute overrides for some test-related annotations. @ContextHierarchy, @WebAppConfiguration, @Rollback, @Repeat, and @ProfileValueSourceConfiguration define single 'value' attributes which cannot be overridden via Spring's meta-annotation attribute support. Although @IfProfileValue has 'values' and 'name' attributes, the typical usage scenario involves the 'value' attribute which is not supported for meta-annotation attribute overrides. Furthermore, 'name' and 'values' are so generic that it is deemed unfeasible to provide meta-annotation attribute override support for these. @BeforeTransaction and @AfterTransaction do not define any attributes that can be overridden. Support for meta-annotation attribute overrides for @Transactional is provided indirectly via SpringTransactionAnnotationParser. Implementation Details: MetaAnnotationUtils.AnnotationDescriptor now provides access to the AnnotationAttributes for the described annotation. MetaAnnotationUtils.AnnotationDescriptor now provides access to the root declaring class as well as the declaring class. ContextLoaderUtils now retrieves AnnotationAttributes from AnnotationDescriptor to look up annotation attributes for @ContextConfiguration and @ActiveProfiles. ContextConfigurationAttributes now provides a constructor to have its attributes sourced from an instance of AnnotationAttributes. ContextLoaderUtils.resolveContextHierarchyAttributes() now throws an IllegalStateException if no class in the class hierarchy declares @ContextHierarchy. TransactionalTestExecutionListener now uses AnnotatedElementUtils to look up annotation attributes for @TransactionConfiguration. Implemented missing unit tests for @Rollback resolution in TransactionalTestExecutionListener. SpringJUnit4ClassRunner now uses AnnotatedElementUtils to look up annotation attributes for @Timed. TestContextManager now retrieves AnnotationAttributes from AnnotationDescriptor to look up annotation attributes for @TestExecutionListeners. DirtiesContextTestExecutionListener now uses AnnotatedElementUtils to look up annotation attributes for @DirtiesContext.

            People

            • Assignee:
              sbrannen Sam Brannen
              Reporter:
              david_syer Dave Syer
              Last updater:
              Sam Brannen
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Days since last comment:
                4 years, 8 weeks, 1 day ago