简体   繁体   中英

How to test only the “key” from .properties file in java using Junit

I have a class ReadPropertyFile which contains method called getPropertyFor which returns a value for the given key as parameter. The getPropertyFor() method I'm calling from other class with key as input parameter. My contents of the properties filetestConfig.properties(file name) are: FILE_NAME=D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx

    public class ReadPropertyFile {


    private Properties properties;
    public ReadPropertyFile(String propertyFileName) {

        properties= new Properties();
        try {
            properties.load(new FileReader(propertyFileName));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public String getPropertyFor(String key) {
        return properties.getProperty(key);
    }
}

@Test
public void testFilePathValueInConfigFile(){
    assertEquals(propertyFileObj.getPropertyFor(keyForFilePath), "D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx");
}

The above test case is to check if the value returned by the function is correct. How do I check if the key is correct? What can be the possible test cases to test the key. And I don't want to change my testConfig.properties file for test purpose. I need help , I'm new to Java Junit testing.

If you want to test wether a key is present in properties file, the there are 2 ways:

If have specified the dafault values for your properties with some predefined values via properies constructor and if you'll try to get the key and it doesn't exist, you'll get this default value back. In that case, you have to check, if your return is equals to this default value.

But if you didn't provide the default, then you can simply use asserNotNull on it's return value, due to javadoc for Propery#getProperty:

Searches for the property with the specified key in this property list. If the key is not found in this property list, the default property list, and its defaults, recursively, are then checked. The method returns null if the property is not found.

But in case of unit testing, you have to provide a number of different properties to test all the possible behaviours of your method or class.

Let's say you have a properties file called myprop.properties:

key1=value1
key2=
key3

You can write the below 4 tests to check if a property is missing or not. As illustrated in different cases, only key (key4) not inside the properties file will return null when you call properties.get("key4"). In the case of key1, it returns value1. In cases of key2 and key3, empty string is returned.

Therefore, to check if a key is present in a properties file, you can use Assert.assertNull(properties.get(KEY_NAME)) .

public class CheckPropertiesTest {
Properties properties = new Properties();

@Before
public void init() throws IOException {
    properties.load(CheckProperties.class.getResourceAsStream("/myprop.properties"));
}

@Test
public void test1() {
    Assert.assertNotNull(properties.get("key1"));
}

@Test
public void test2() {
    Assert.assertNotNull(properties.get("key2"));
}

@Test
public void test3() {
    Assert.assertNotNull(properties.get("key3"));
}

@Test
public void test4() {
    Assert.assertNull(properties.get("key4"));
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM