Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deal with the test data in Junit?

In TDD(Test Driven Development) development process, how to deal with the test data? Assumption that a scenario, parse a log file to get the needed column. For a strong test, How do I prepare the test data? And is it properly for me locate such files to the test class files?

like image 321
Joseph Avatar asked Jun 01 '10 10:06

Joseph


People also ask

What is test data in Java?

Test data is defined in the @Parameters annotation, which is associated with the test itself, not the class, and passed to the test via method parameters.

Do we have data provider in JUnit?

Apparently JUnit does not provide support for data providers, which makes it rather annoying to test the same method with 20 different versions of an argument.


3 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'll want to place the test resources in the right folder, if your building with something different, you may want to use this structure as it is more than just a maven convention, to my opinion it's close to 'best practise'.

like image 102
Andreas Dolk Avatar answered Oct 09 '22 06:10

Andreas Dolk


Another option is to mock out your data, eliminating any dependency on external sources. This way it's easy to test various data conditions without having to have multiple instances of external test data. I then generally use full-fledged integration tests for lightweight smoke testing.

like image 3
puug Avatar answered Oct 09 '22 08:10

puug


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

Create the test data from a real log file. Write a list of the tests intended to be written, tackle them one by one and tick them off once they pass.

like image 3
philant Avatar answered Oct 09 '22 08:10

philant