简体   繁体   English

如何使用Junit在Java中仅测试.properties文件中的“密钥”

[英]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. 我有一个类ReadPropertyFile,它包含名为getPropertyFor的方法,该方法返回给定键的值作为参数。 The getPropertyFor() method I'm calling from other class with key as input parameter. 我正在以键作为输入参数从其他类调用getPropertyFor()方法。 My contents of the properties filetestConfig.properties(file name) are: FILE_NAME=D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx 我的属性filetestConfig.properties(文件名)的内容是: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. 而且我不想出于测试目的更改我的testConfig.properties文件。 I need help , I'm new to Java Junit testing. 我需要帮助,我是Java Junit测试的新手。

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. 如果已通过属性构造函数为属性指定了dafault值并带有一些预定义的值,并且如果尝试获取该键且该键不存在,则将获得此默认值。 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: 但是,如果您未提供默认值,则由于Propery#getProperty的javadoc,您可以在其返回值上直接使用asserNotNull:

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. 如果找不到该属性,则该方法返回null。

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: 假设您有一个名为myprop.properties的属性文件:

key1=value1
key2=
key3

You can write the below 4 tests to check if a property is missing or not. 您可以编写以下4个测试来检查属性是否缺失。 As illustrated in different cases, only key (key4) not inside the properties file will return null when you call properties.get("key4"). 如在不同情况下所示,当您调用properties.get(“ key4”)时,只有不在属性文件中的键(key4)会返回null In the case of key1, it returns value1. 对于key1,它返回value1。 In cases of key2 and key3, empty string is returned. 如果是key2和key3,则返回空字符串。

Therefore, to check if a key is present in a properties file, you can use Assert.assertNull(properties.get(KEY_NAME)) . 因此,要检查属性文件中是否存在密钥,可以使用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"));
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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