Test data directory with jUnit

I am writing several jUnit tests that depend on data files. Where should these data files go? And how can I (in jUnit tests) get the location of this directory?

In Python, I would use something similar to:

datadir = os.dirname(__file__) + "/data/" 
+6
java unit-testing junit
source share
2 answers

Partly depends on what you use the data files for, but in general, just create a package and make sure that it is on the path to the class. To load the properties file from the "data" package, add the file "MyData.props" and you can load the properties file, for example:

 this.getClass().getClassLoader().getResourceAsStream("/data/MyData.props"); 

Again, it’s not entirely accurate whether this question will answer your question, since I’m not 100% sure what you are trying to do, but I hope this helps a little.

+8
source share

Store test data next to your test classes (same package). As todd.run suggested, use getResourceAsStream() to access your data files.

+2
source share

All Articles