Вопрос

I have a unit test with an InputStream capture. Something like this:

Capture<InputStream> capturedArgument = new Capture<InputStream>();
// some expectations and the tested method execution here
InputStream actualStream = capturedArgument.getValue();
// some assertions here

The actual stream (which is captured in the test) is closed in the tested object. Should I manually close the actualStream after the assertions in the unit test? Or it will be closed too after actual stream closing?

Thank you.

Это было полезно?

Решение

its always a good decision to close resources, to keep them available for other threads and avoid leaks but this is a test, and the jvm will be closed after the test anyway so its done and you can leave it as it is to keep the test pretty small.

In this case especially, it is already closed, because you mentioned that the close method will be called in the application itself. So the Stream inside the capture is also closed.

I would guess you have the following call hierarchy:

in junittest

  • call businesscode

in businesscode to test

  • open stream
  • call method(inputStream)

junitcode in captured object

  • get value from capture
  • assert something

back in businesscode

  • close stream within a finally

junitcode

  • end of test

stream is closed

by the way, you should get an already Closed message inside an IOException if you try to close it more then once, so you can try both ways, right?

Другие советы

It'll close itself eventually, most finalize methods call close methods after all. Still it's probably best to do it manually since there is no garantee of when the garbage collector will call finalize.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top