Description
Sam Brannen opened SPR-7827 and commented
Overview
Spring 3.0 already allows component stereotypes to be used in a meta-annotation fashion, for example by creating a custom @TransactionalService
stereotype annotation which combines @Transactional
and @Service
in a single, reusable, application-specific annotation.
As mentioned in various forums, a JIRA comment, and discussions I've had with Spring users, it would be beneficial to provide similar meta-annotation support for test-related annotations.
Given a custom @TransactionalTest
stereotype annotation (see code listing below), multiple test classes in the application's test suite could rely on centralized configuration of the context and transaction configuration and thus avoid unnecessary duplication. See UserRepositoryIntegrationTests
for an example.
Notes on JUnit
Naturally, Spring cannot provide meta-annotation support for JUnit's @RunWith
annotation, since @RunWith
is processed by JUnit internals. Developers would therefore still be required to specify SpringJUnit4ClassRunner
as the runner for each individual test class.
Deliverables
Provide meta-annotation support for the following annotations within the context of the TestContext framework.
-
@ContextConfiguration
-
@WebAppConfiguration
-
@ContextHierarchy
-
@ActiveProfiles
-
@DirtiesContext
-
@TestExecutionListeners
-
@IfProfileValue
-
@ProfileValueSourceConfiguration
-
@Transactional
-
@BeforeTransaction
-
@AfterTransaction
-
@TransactionConfiguration
-
@Rollback
-
@Repeat
-
@Timed
Code Examples
@Transactional
@ContextConfiguration({"/app-config.xml", "/test-data-access-config.xml"})
public @interface TransactionalTest {}
@TransactionalTest
@RunWith(SpringJUnit4ClassRunner.class)
public class UserRepositoryIntegrationTests { /* ... */ }
Further Resources
- JUnit: All JUnit annotations should be able to be applied as meta-annotations
- JUnit: @RunWith(MetaAnnotations.class)
Affects: 3.0.5
Issue Links:
- Document meta-annotation support in the TestContext framework [SPR-11109] #15735 Document meta-annotation support in the TestContext framework ("is depended on by")
- ContextLoader is supplied composed annotation class instead of test class for default resource detection [SPR-11455] #16081 ContextLoader is supplied composed annotation class instead of test class for default resource detection
- Support composed annotations declared on interfaces [SPR-11108] #15734 Support composed annotations declared on interfaces
- Support meta-annotation attribute overrides in the TestContext framework [SPR-11038] #15666 Support meta-annotation attribute overrides in the TestContext framework
Referenced from: commits 5e7021f, 2bd5a53, 412f74f
8 votes, 11 watchers