traveh
traveh

Reputation: 2894

Where to store a binary file to be used for unit testing

Where should I store a resource (binary file in my case) that I need to use for unit testing?

Specifically, my directory structure is a standard:

src -> main -> java -> com.company.project -> classes...
    -> test -> java -> com.company.project -> classes...

I want to mock a file being read from the file system and replace it with a file from within my project.

Upvotes: 2

Views: 2223

Answers (2)

Raedwald
Raedwald

Reputation: 48702

I consider needing a binary resource for unit testing as a code smell. It suggests that the API of your code is not expressive enough: all it can take as input is a blob of bytes. What is in this resource? Is it really just a stream of bytes,or does it have some structure?

If your code parses this binary input, consider instead also writing code for writing this binary format as output, then test that writing-followed-by-reading is symmetric. If you do not want your code to provide functionality for writing the binary format, make those methods package-private, rather than public, or put them in a class that you use only for testing.

If your code does not parse this binary input, but instead calls some other code to parse the binary and uses the result of that parsing, alter the API of your code so its input is that parsed data.

Upvotes: 0

crea1
crea1

Reputation: 12617

src\test\resources is a good place for resources you need for unit-testing, if you are using mavens standard directory layout.

Read more about the different directories here: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

Upvotes: 4

Related Questions