-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
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:
Start TestSuite
Start Test1
... Bean.initMethod()
... Bean.destroyMethod()
End Test1
Start Test2
... Bean.initMethod()
... Bean.destroyMethod()
End Test2
End TestSuite
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,
Start TestSuite
Start Test1
... Bean.initMethod()
End Test1
Start Test2
... Bean.initMethod()
End Test2
Bean.destroyMethod() (caused by Test2)
Bean.destroyMethod() (caused by Test1)
End TestSuite
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:
$ ant | grep method
jcheng@jackrabbit: ~/lms-projects/DEV_SPRING_3/Spring_Bug_Report_01 $ ant | grep method
[junit] method: LifeCycleBean[0].init()
[junit] method: SampleJunitTest1.testOne()
[junit] method: LifeCycleBean[1].init()
[junit] method: SampleJunitTest2.testTwo()
[junit] method: LifeCycleBean[1].destroy()
[junit] method: LifeCycleBean[0].destroy()
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:
- Spring_Bug_Report_01.tar.bz2 (1.96 MB)
Issue Links:
- ANT does not call destroy-method after tests [SPR-2007] #6700 ANT does not call destroy-method after tests
- @PreDestroy method in test instance not executed when run with SpringJUnit4ClassRunner [SPR-4868] #9544
@PreDestroymethod in test instance not executed when run with SpringJUnit4ClassRunner
2 votes, 2 watchers