Skip to content
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

context marked dirty using @DirtiesContext in TestNG based test is not reloaded in subsequent tests [SPR-3880] #8560

Closed
spring-projects-issues opened this issue Sep 14, 2007 · 2 comments
Assignees
Labels
type: bug A general bug
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

kai lilleby opened SPR-3880 and commented

running the following test:
@ContextConfiguration(locations = {"....spring.xml"})
public class TestngTest extends AbstractTransactionalTestNGSpringContextTests{

@Test
@DirtiesContext
public void testMethod1() {
}

@Test
@DirtiesContext
public void testMethod2(){
}

}

the springcontext will be closed in the first test as a result due to @DirtiesContext, but no new context will be loaded before running the second test, hence code depending on the existence of a valid context will fail.

Is this intended?
does context reloading onoly work accross different test classes, ie. not inside one and the same test class?

According to the spring documentation (http://static.springframework.org/spring/docs/2.1.x/reference/testing.html#testcontext-framework):
"you may annotate your test method with @DirtiesContext (assuming DirtiesContextTestExecutionListener has been configured, which is the default) to cause the test fixture to reload the configurations and rebuild the application context before executing the next test case." - it seems like my test should have worked.. - or?

kai:)


Affects: 2.5 RC1

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Hi Kai,

the springcontext will be closed in the first test as a result due to @DirtiesContext,
but no new context will be loaded before running the second test, hence code depending
on the existence of a valid context will fail.

That is partially correct:

After careful analysis, it turns out that the ApplicationContext is in fact closed and
recreated as expected; however, due to how the test instance is instrumented with the
TestContextManager in AbstractTestNGSpringContextTests, dependency injection is not being
performed on the test instance between individual test methods with TestNG.

Thus, although a new ApplicationContext is being created, it is not being supplied
to your test instance via ApplicationContextAware semantics. In other words,
this.applicationContext references the old, closed, dirtied context, which is
presumably not what you desire.

does context reloading onoly work accross different test classes, ie. not inside one
and the same test class?

Well, it works with JUnit 3.8 and JUnit 4.4, because JUnit instantiates a new instance
of your test class between test method execution; however, as you indirectly noticed,
it does not work as you might have expected with the abstract TestNG base test classes.

I'll have a look into a possible work-around or solution for you and get back to you.

Thanks for bringing this issue to my attention.

Sam

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

The Spring TestContext Framework's dependency injection support and handling of
@DirtiesContext have been refactored so that all dependencies (including an
ApplicationContext set via ApplicationContextAware semantics) will be "reinjected"
into the test instance after any test method which declares @DirtiesContext.

Please give one of the upcoming nightly builds a try to see if this meets your needs.

Thanks,

Sam

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

No branches or pull requests

2 participants