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

爱⌒轻易说出口 提交于 2019-12-11 03:52:24

问题


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.


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/30595038/where-to-store-a-binary-file-to-be-used-for-unit-testing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!