Skip to content

Introduce autowiring support for the current TestContext [SPR-8299] #12947

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
spring-projects-issues opened this issue May 6, 2011 · 1 comment
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented May 6, 2011

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:

@sbrannen
Copy link
Member

Closing this issue due to lack of interest from the community.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants