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

Support for @DirtiesContext at the test class level [SPR-4702] #9379

Closed
spring-projects-issues opened this issue Apr 14, 2008 · 9 comments
Closed
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Apr 14, 2008

Noel Flicken opened SPR-4702 and commented

Expected behavior:
Context should be close()'d after end of test class execution

Observed behavior:
Context only closed when @DirtiesContext annotates method

Multiple test classes can re-use the same context, which allows for faster test execution, so automatically closing a context after test class execution is probably not the best mechanism.

However, there should be a way to annotate that a context should be close()'d after all tests in a test class are executed.

Suggested fix:

  • allow @DirtiesContext to annotate class
  • add TestExecutionListener#destroyTestInstance

Willing to implementing, if given approval of design.


Issue Links:

Referenced from: commits 2dee54b, e77e070, 7782184, 0483cb5, 8dec6af, 1f087b4, 51b8b99, f26e2e3

4 votes, 6 watchers

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented May 29, 2008

Noel Flicken commented

This issue differs from #6700 in expected implementation. SPR_2007 is difficult to implement due to JUnit architectural limitations.

This issue suggests a feature to optionally allow a context to be automatically closed after all test methods in a given Test Class are run, i.e. when JUnit @AfterClass methods are run.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

OK, good point - although I'm not entirely clear how exactly such @AfterClass closing will fit with context sharing. We'll see.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Noel Flicken commented

Fair enough. Allowing @DirtiesContext to annotate a test class has a clear semantic meaning, and is a natural extension to its currently method-only usage. This means that the interaction between @DirtiesContext and context-sharing would be unchanged from the current implementation.

@spring-projects-issues
Copy link
Collaborator Author

Eric Jain commented

This would also be beneficial for TestNG tests (e.g. using AbstractTestNGSpringContextTests) where one would like to make use of TestNG's support for explicit dependencies between test methods. Not being able to control context reloading at the class level makes this feature a lot less useful (and the functional tests a lot uglier).

@spring-projects-issues
Copy link
Collaborator Author

Olaf Otto commented

I would also like a convenient way to enforce context destruction after test class execution. In my specific case, I have observed that the refresh() that takes place on test class execution creates a seemingly new context but does not dispose old singletons (those are destroyed after all test classes have finished or if an exception is thrown...) which causes a significant memory consumption...

@spring-projects-issues
Copy link
Collaborator Author

Joost den Boer commented

Destroying the context would be very welcome. I have several testcases with multiple testmethods which, when using @DirtiesContext on method level, cause test failures because when creating a new context for the last testmethods no new db2 connections can be made. Apparently a problem with our db2 servers which cannot handle setting up and destroying connections so fast. Destroying the whole context would be a great solution for this problem.

I tried to work around it by creating a @DirtiesBean annotation which just destroys the one bean I had used in the test. It seemed to work fine but I now run into different destroy behaviour between running in Maven and in Eclipse. (See http://forum.springsource.org/showthread.php?p=240436#post240436)

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

@DirtiesContext is now supported at the class level as well as at the method level.

This is made possible via new support for 'before class' and 'after class' callbacks in the TestExecutionListener interface. DirtiesContextTestExecutionListener now implements afterTestClass(TestContext) which provides the class-level support for @DirtiesContext.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Note: due to the limitations of JUnit 3.8, @DirtiesContext is not supported at the class level for tests run with subclasses of AbstractJUnit38SpringContextTests.

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

Noel, Eric, Olaf, Joost, et al,

Please try out this new functionality in one of the upcoming nightly snapshots or in 3.0.0.RC1, and let me know if this meets your needs. Note that using @DirtiesContext at the class level should be fully supported in JUnit 4.6 and TestNG 5.9.

Thanks for all of your input!

Sam

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