JUnit - should I assign null to resources in tearDown that were instantiated in setUp?

前端 未结 3 657
暖寄归人
暖寄归人 2021-01-11 11:07

I am reading one book about JUnit now and writer advises nulling resources in tearDown method. Why? Isn\'t this GC\'s job? Can it seriously make any harm?

Lets think

3条回答
  •  [愿得一人]
    2021-01-11 11:36

    Yes, this can indeed be necessary.

    You see, JUnit will actually create a separate instance of the Test class for each test method, and the Junit3 test runner (not so with JUnit4) will keep these instances around until the entire test suite has finished.

    Therefore, if your (JUnit3) test class has fields that take up a lot of memory, you can easily run out of heap space when you have a large number of test methods. Of course, if those collections in your example code only ever contain a handful of short strings, it doesn't matter.

提交回复
热议问题