David Wolever
David Wolever

Reputation: 154682

Test data directory with jUnit

I'm writing some jUnit tests that depend on data files. Where should those data files go? And how would I (in the jUnit tests) get the location of that directory?

In Python, I would use something similar to:

datadir = os.dirname(__file__) + "/data/"

Upvotes: 6

Views: 3274

Answers (2)

Todd R
Todd R

Reputation: 18526

Kind of depends on what you're using the data files for, but in general, just create a package and make sure it's on your classpath. To load a properties file from the "data" package, add a "MyData.props" file and you can use load a properties file like:

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

Again, not exactly sure if this answers your question since I'm not 100% sure what you're trying to do, but I hope it helps a little.

Upvotes: 8

trunkc
trunkc

Reputation: 6293

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

Upvotes: 2

Related Questions