Skip to content

When testing using JUnit 4 Suite, destroy-methods are called at the end of the test suite instead of per test [SPR-7377] #12035

@spring-projects-issues

Description

@spring-projects-issues

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:

Issue Links:

2 votes, 2 watchers

Metadata

Metadata

Assignees

Labels

in: testIssues in the test modulestatus: declinedA suggestion or change that we don't feel we should currently applytype: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions