How to work with test data in Junit?

During the development of TDD (Test Driven Development), how to work with test data? Assuming the script, parse the log file to get the column you want. For a strong test How to prepare test data? And is it right for me to find such files in test class files?

+6
java unit-testing junit tdd
Jun 01 '10 at
source share
5 answers

Maven, for example, uses a convention for folder structures that takes care of test data:

src main java <-- java source files of main application resources <-- resource files for application (logger config, etc) test java <-- test suites and classes resources <-- additional resources for testing 

If you use maven for building, you will want to place the test resources in the right folder, if your building is with something else, you can use this structure, since it is more than just a maven agreement, in my opinion this is close to "best practice "

+14
Jun 01 '10 at 10:35 on
source share

Hard code them in tests so that they are close to the tests that use them, making the test more readable.

Create test data from a real log file. Write a list of tests that need to be written, solve them one by one and mark them after passing them.

+2
Jun 01 2018-10-06T00:
source share

Another option is to mock your data, eliminating dependence on external sources. Thus, it is easy to test different data conditions without having to have multiple instances of external test data. Then I usually use full integration tests for easy smoke testing.

+2
Jun 01 '10 at 11:45
source share
 getClass().getClassLoader().getResourceAsStream("....xml"); 

worked for me inside the test. But

 getClass().getResourceAsStream("....xml"); 

did not work. I don't know why, but maybe this helps some others.

+1
Aug 13 '14 at 9:01
source share

When my test data should be an external file - a situation that I try to avoid, but not always - I put it in the reserved test data directory at the same level as my project and use getClass().getClassLoader().getResourceAsStream(path) for read it. A test data catalog is not a requirement, just a convenience. But try to avoid it; as @philippe points out, it's almost always better to have values ​​hard-coded in tests right where you can see them.

0
Jun 01 '10 at 13:47
source share



All Articles