Question

I am developing an application that tests web Services, and I use JUnit parameterized tests. I want to read parameters from a resources file. I am wondering which is the best way to store these parameters.

  1. In a .properties file ?

    test1.inputPath= C:\\Path
    test1.expectedOutputPath= C:\\Path
    test2.inputPath= C:\\Path2
    test2.expectedOutputPath= C:\\Path2
    
  2. In an xml file?

    <test>
      <inputPath>  C:\Path <\inputPath>
      <expectedOutputPath>  C:\Path <\expectedOutputPath>
    <\test>
    <test>
      <inputPath>  C:\Path2 <\inputPath>
      <expectedOutputPath>  C:\Path2 <\expectedOutputPath>
    <\test>
    
  3. Other way to do this?

Thanks.

Was it helpful?

Solution 2

The best solution that I found for my problem is to use PropertiesConfiguration of Apache Commons Configuration. It is very simple to use:

In my .properties file :

test1= Path1,Path2
test2= Path3,Path4

Then I read the .properties file automatically and I retrieve paths as a String array for each test.

@Parameters
public static Collection<Object[]> readPropertiesFile(){
    ArrayList<Object[]> result= new ArrayList<Object[]>();
    try {
        Configuration config = new PropertiesConfiguration("testPaths.properties");
        Iterator<String> keys=config.getKeys();
        while(keys.hasNext()){
            String[] paths= config.getStringArray(keys.next());
            result.add(paths);          
        }
    } catch (ConfigurationException e) {
        e.printStackTrace();
    }
    return result;
}

OTHER TIPS

Do not try to make your life more complicated ;) You can easily read property files in this way:

Properties prop = new Properties();
InputStream input = new FileInputStream(fileName);
prop.load(input);
String s = prop.getProperty("test1.inputPath");

and import:

import java.util.Properties;

is it still complicated for you?

The answer is, of course, there are MANY ways of doing it.

First ask yourself: do these properties will change? Are they parameters or are they constants? For example, the number of the states, what are the chances they will change? In this case you need a constant, not a parameter.

Now, if you are looking for something that can changed at runtime then you should look into properties and resource bundles.


If all you need are constants, then you can do something like:

public interface Constants
{
    public char NUMBER_ONE = '1';
    public long A_LONG_TIME_AGO = 1321322;
    public String CANT_BREAK_WITH_IRON_PICKAXE= "OBSIDIAN";
}

There are many advantages in using interfaces: they don't need to be instantiated, won't slow down your system with IO access, and all attributes are static final.


However, if you need to load the values during runtime, then use properties files. Although all answers here are good, the only one I consider good enough is Spring's @Configuration and @ImportResource, which are injected, allow for nice mocking, and integrate nicely with the rest of the Spring framework, and can be easily overridden with -D from the command line.

Example on how to load properties files using a mix of xml and properties files: Spring-Boot: How do I reference application.properties in an @ImportResource

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top