Perhaps the simplest way to organize test data is to place it in the same package with test cases and access the data as resources. If the resources are located in a directory (as opposed to a JAR file), then Repo allows to access their data not only as resources, but also as files (i.e., java.io.File objects).
Class junit.extensions.repo.PackageRepo allows to access package resources as files. Class PackageRepo extends class Repo, so it should be constructed with setUp() method as follows:
protected void setUp() { repo = new PackageRepo(); }
Suppose that class MyTest (see section 3.2) has a resource with the name test.properties in the same package. Java allows to access that resource as:
import java.io.InputStream; import java.net.URL; ... URL url = MyTest.class.getResource( "test.properties"); InputStream in = MyTest.class.getResourceAsStream( "test.properties");
In order to access test.properties through PackageRepo, the following should be used:
import java.io.File; import java.io.InputStream; import java.net.URL; ... File file = repo.getFile("test.properties"); URL url = repo.getResource("test.properties"); InputStream in = repo.getResourceAsStream( "test.properties");
As observed, PackageRepo provides access to test.properties as java.io.File.
The advantage of using PackageRepo to access resources as files is that the test class does not need to know its location in the file system or its package structure. PackageRepo determines this information transparently.
See also: