2
I have the following directory structure for the Unit tests Resource folder:
src/test/resources/*.files
So that I can read binary files (like a PDF for example) to complete a Unit test, I am colonizing these files in the test/Resources folder, but reading the Maven documentation, this directory is suitable for files that are uploaded in Runtime, and such files would be called with getClass().getClassLoader().getResourceAsStream()
.
What would be the best practice for storing and reading PDF files that will be used for testing on one or more Unit test?
Besides getClass(). getResource(), can I use Fileinputstream? Pq usually use getClass(). getResource() and not a File, Fileinputstream and the like for reading files in Unit test?
– Gustavo Piucco
It’s okay to use the
FIleInputStream
, what you have to realize is that your unit test has to run on any machine. If you use theFileInputStream
, you cannot use a path that only exists on your machine. ThegetResource
will look for the file in the project’s Resources folder regardless of which directory the project is in. If the required files are also committed, anyone who downloads your project will be able to run unit tests.– Fagner Fonseca
The same behavior can be achieved using the
FIleInputStream
, but it can take more work because you will have to mount the path in hand. The correct thing is to put the files in the Resources folder, so it doesn’t make much sense not to use the methods that already search for the files there.– Fagner Fonseca