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
When testing using JUnit 4 Suite, destroy-methods are called at the end of the test suite instead of per test [SPR-7377] #12035
Comments
Sam Brannen commented John, have you tried annotating either your test class or test methods with That should give you the behavior you desire. So please try it out and report back here with your findings. Thanks, Sam |
Conny Kreyßel commented Why is " If i run in eclipse a folder full of spring junit4 based tests, i run out of memory because every single application context of every test class is hold in memory. If you have hibernate/ehcache/lucene on board, you get also problems with threads that run out, because it would not shutdown it resources. Hints? |
Paul Khodchenkov commented I am also surprised that spring does not close application context after the test class. |
Sam Brannen commented Conny,
The default behavior of the TestContext framework is to cache all application contexts within the currently executing process (i.e., typically in a test run within the IDE or a project build in Maven, Ant, Gradle, etc.). There is good reason for this: one of the primary objectives of the TestContext is caching for faster build times. Please see the reference manual for details: Of course, if you have not designed your test classes to reuse the same application context (i.e., by declaring the same uniquely identifying configuration (e.g., locations, classes, etc.)), then the TestContext will create and cache a new So you have two options:
I hope this helps to shed some light on the intended use cases for the TestContext framework with regard to caching of application contexts. Regards, Sam |
Sam Brannen commented Paul, Please see my response to Conny as it applies directly to your comments as well. Regards, Sam |
Sam Brannen commented I am closing this issue as Works as Designed since this reflects the intended behavior. For details, please refer to my comments regarding caching in the TestContext framework. |
John Cheng opened SPR-7377 and commented
Expected Behavior
When running multiple test classes using JUnit4's Suite runner, I expect a Spring bean's destroy-method to be called at the end of each test. In other words, I expect to see:
This is because I need resources to be freed after Test1, so that I can cleanly re-acquire those resources when Test2 starts.
Current Behavior
A bean's init-method is called before each test. However, the destroy-method is called at the end of the test suite, in one batch. In other words,
In the current behavior, resources acquired in Test1 will not be freed via the destroy-method when Test2 starts.
Steps to reproduce
I've attached sample code. It is in the form of an Eclipse project, but can be executed via ant:
Note that in the stderr output (when you run Ant), the LifeCycleBean's destroy-method is not called until the test suite ends.
Affects: 3.0.3
Attachments:
Issue Links:
@PreDestroy
method in test instance not executed when run with SpringJUnit4ClassRunner2 votes, 2 watchers
The text was updated successfully, but these errors were encountered: