JUnit check for reading JSON files

Suppose I want to write a test for a Java class that provides a method for reading and analyzing external files (more precisely, the files will be JSON, and I will use Jackson).

In addition, I have some examples of JSON files that I have analyzed, and I also have a vague idea of ​​which Java object this SomeMagicalReader.readPony("path/to/location/pony.json") method SomeMagicalReader.readPony("path/to/location/pony.json") should return; if I manage to get readPony to return some kind of PonyObject , I think there is an idea how to verify that the produced PonyObject is what I expect.

The question I have is to provide readPony function readPony test data. I probably think too much about this, but (1) is there an idiomatic way of "Java + Junit"? (= testing a method that reads external files?). Copy the contents of an example file as a String variable in test code? (They are quite short, but it will still look pretty ugly.) Put an example of JSONs ... somewhere and call readPony using the path? (This sounds more reasonable.) (2) What would then be a canonical place to host such external JSON test files if my tests are organized in a hierarchy of Maven-style test packages, for example. src/test/java/com/stuff/app/package/SomeMagicalReaderTest.java ?

+7
java json maven file-io junit
source share
3 answers

According to the maven standard directory layout , I would advise you to place the JSON test files in src/test/resources , since they are test resources.Note that you can (and should) organize your own hierarchy of folders in the resource folder (other it would be easier for developers to find specific test resources when fixing or adding some other tests).

So yes, you will get JSON files somewhere, but not somewhere, if your own hierarchy of test resources is good enough (for example, if you think that the structure of your package is well organized with meaningful package names, following its test resources hierarchy not a bad idea at all).

+9
source share

You should ask yourself what is the critical mission code for your project β€” reading files or analyzing their contents. For my projects, the analysis was interesting, so I put the files for analysis as test resources, read them in the unit test line and passed them to the parser in the unit test parser. You can also include content directly in unit tests like big ugly strings, but when you have dedicated space for test resources, why not use it.

+3
source share

Copy the contents of the sample file as a string variable in the test code

I suggest that you do not do this, since it makes it difficult to make changes to the source data for your tests. In addition, using an external file makes your tests more flexible. For example, reading an external file allows you to create multiple tests when reusing the base structure for each test. Of course, this also means that you will need to spend some time developing methods that actually perform tests.

+2
source share

All Articles