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

前端 未结 3 670
暖寄归人
暖寄归人 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:48

    It depends what you consider a resource. Whilst heap space is a resource, you can probably get away with the GC cleaning up after you (YMMV).

    Things that might cause issues are Closables like database connections / open files and streams etc. which should always be closed after use to prevent nasties in long running code.

    I once had a situation that an integration test for some hibernate code didn't cleanup properly and resulted in some really strange errors. It took many hours to find and angered me so badly that I'll never make the same mistake again.

提交回复
热议问题