-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Closed
Labels
in: testIssues in the test moduleIssues in the test moduletype: enhancementA general enhancementA general enhancement
Description
Sam Brannen opened SPR-8299 and commented
Status Quo
Tests currently do not have access to the current TestContext.
Providing access to the TestContext would allow programmatic access to TestContext properties as well as methods such as markApplicationContextDirty().
Considerations
Instead of exposing the TestContext as is, it may be preferential to expose a new (yet-to-be-implemented) public-facing interface that defines a subset of the publicly available methods in TestContext.
A similar approach could be introduced to provide programmatic support for transaction management.
Code Samples
DependencyInjectionTestExecutionListener's injectDependencies(TestContext) method could be modified as follows:
protected void injectDependencies(final TestContext testContext) throws Exception {
Object bean = testContext.getTestInstance();
ApplicationContext applicationContext = testContext.getApplicationContext();
// BEGIN NEW CODE
// Enable autowiring of the current TestContext.
if (applicationContext instanceof ConfigurableApplicationContext) {
ConfigurableApplicationContext configurableApplicationContext = (ConfigurableApplicationContext) applicationContext;
configurableApplicationContext.getBeanFactory().registerResolvableDependency(TestContext.class, testContext);
}
// END NEW CODE
AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();
beanFactory.autowireBeanProperties(bean, AutowireCapableBeanFactory.AUTOWIRE_NO, false);
beanFactory.initializeBean(bean, testContext.getTestClass().getName());
testContext.removeAttribute(REINJECT_DEPENDENCIES_ATTRIBUTE);
}public class MyTests {
@Autowired
protected TestContext testContext;
@Test
public void test() {
assertNotNull(testContext);
assertEquals("test", testContext.getTestMethod().getName());
}
}Affects: 2.5.6
Issue Links:
- Support programmatic starting and stopping of transactions in the TestContext framework [SPR-5079] #9753 Support programmatic starting and stopping of transactions in the TestContext framework
Metadata
Metadata
Assignees
Labels
in: testIssues in the test moduleIssues in the test moduletype: enhancementA general enhancementA general enhancement