24

I have JUnit tests that need to run in various different staging environments. Each of the environments have different login credentials or other aspects that are specific to that environment. My plan is to pass an environment variable into the VM to indicate which environment to use. Then use that var to read from a properties file.

Does JUnit have any build in capabilities to read a .properties file?

Dave Jensen
  • 4,574
  • 1
  • 40
  • 45

5 Answers5

32

It is usually preferred to use class path relative files for unit test properties, so they can run without worrying about file paths. The path may be different on your dev box, or the build server, or where ever. This will also work from ant, maven, eclipse without changes.

private Properties props = new Properties();

InputStream is = ClassLoader.getSystemResourceAsStream("unittest.properties");
try {
  props.load(is);
}
catch (IOException e) {
 // Handle exception here
}

putting the "unittest.properties" file at the root of the classpath.

scott m gardner
  • 519
  • 5
  • 12
  • 2
    This is good information. You might consider making this a comment to the already accepted answer; there it would be far more likely to be read by someone who comes across this in their results. – chb Aug 10 '12 at 04:20
  • 3
    This is the correct solution if you put your properties files in `src/test/resources` – ACV Feb 25 '19 at 12:25
29

java has built in capabilities to read a .properties file and JUnit has built in capabilities to run setup code before executing a test suite.

java reading properties:

Properties p = new Properties();
p.load(new FileReader(new File("config.properties")));

junit startup documentation

put those 2 together and you should have what you need.

pstanton
  • 35,033
  • 24
  • 126
  • 168
1

This answer is intended to help those who use Maven.

I also prefer to use the local classloader and close my resources.

  1. Create your test properties file, called /project/src/test/resources/your.properties

  2. If you use an IDE, you may need to mark /src/test/resources as a "Test Resources root"

  3. add some code:


// inside a YourTestClass test method

try (InputStream is = loadFile("your.properties")) {
    p.load(new InputStreamReader(is));
}

// a helper method; you can put this in a utility class if you use it often

// utility to expose file resource
private static InputStream loadFile(String path) {
    return YourTestClass.class.getClassLoader().getResourceAsStream(path);
}
Barett
  • 5,826
  • 6
  • 51
  • 55
1
//
// Load properties to control unit test behaviour.
// Add code in setUp() method or any @Before method (JUnit4).
//
// Corrected previous example: - Properties.load() takes an InputStream type.
//
import java.io.File;
import java.io.FileInputStream;        
import java.util.Properties;

Properties p = new Properties();
p.load(new FileInputStream( new File("unittest.properties")));

// loading properties in XML format        
Properties pXML = new Properties();
pXML.loadFromXML(new FileInputStream( new File("unittest.xml")));
edvox1138
  • 21
  • 1
0

If the aim is to load a .properties file into System Properties, then System Stubs (https://github.com/webcompere/system-stubs) can help:

The SystemProperties object, which can be used either as a JUnit 4 rule to apply it within a test method, or as part of the JUnit 5 plugin, allows setting properties from a properties file:

SystemProperties props = new SystemProperties()
    .set(fromFile("src/test/resources/test.properties"));

The SystemProperties object then needs to be made active. This is achieved either by marking it with @SystemStub in JUnit 5, or by using its SystemPropertiesRule subclass in JUnit4, or by executing the test code inside the SystemProperties execute method.

Ashley Frieze
  • 4,993
  • 2
  • 29
  • 23