detecting resource leak in production code via tests

54 Views Asked by At

i am trying to find if there's a way to detect resource leak in tests. with junit or any other library.

last time, i was using junit5 @TempDir and i had my outputstream not closed it was throwing unable to delete temporary folder. it didn't listed the correct reason, nor i could find online. i just guessed and i checked if i am close my FileOutputStream and it was it. then it started working. no exception by junit.

but this is not very useful. is there a good way to find in tests if any resource is not closed.

2

There are 2 best solutions below

0
AndrewL On

General resource leaks

Consider static code analysis for the problem of detecting resource leaks generally. For instance Sonar has some help: https://rules.sonarsource.com/java/tag/leak/RSPEC-2095/

Leaks specific to JUnit

Sure, static code analysers may not spot problems where the resource is undetectable, e.g. in the case of @TempDir, so you should suggest this directly the JUnit team... provide a PR even!

1
duckydude20 On

recently found a great tool, rather than tests it's more of a linter. or static analyzer as they say.

https://checkerframework.org/

i have not tried it, but it seems good.