How to deal with the test data in Junit?
Asked Answered
S

5

7

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?

Subulate answered 1/6, 2010 at 10:11 Comment(0)
B
16

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'.

Bacterin answered 1/6, 2010 at 10:35 Comment(2)
Is this a test convention in Maven?Subulate
Maven simplifies the build process for Java applications and makes big use of conventions like folders structures. If a project follows those conventions, then the build files will be pretty small because maven 'nows' what to do based on the files and folders it sees. (just in brief)Bacterin
R
3

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.

Reprehensible answered 1/6, 2010 at 11:45 Comment(0)
G
2

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.

Gokey answered 1/6, 2010 at 10:38 Comment(0)
R
1
getClass().getClassLoader().getResourceAsStream("....xml");

inside the test worked for me. But

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

didn't worked. Don't know why but maybe it helps some others.

Rosebay answered 13/8, 2014 at 9:1 Comment(0)
K
0

When my test data must be an external file - a situation I try to avoid, but can't always - I put it into a reserved test-data directory at the same level as my project, and use getClass().getClassLoader().getResourceAsStream(path) to read it. The test-data directory isn't a requirement, just a convenience. But try to avoid needing to do this; as @philippe points out, it's almost always nicer to have the values hard-coded in the tests, right where you can see them.

Karie answered 1/6, 2010 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.