Java 如何测试;“关键”;及;“价值”;config.properties文件的名称?

Java 如何测试;“关键”;及;“价值”;config.properties文件的名称?,java,testing,junit4,properties-file,Java,Testing,Junit4,Properties File,我有一个类ReadPropertyFile,它有一个返回类型string的方法getPropertyFor()(这是作为参数传递的键对应的值)。我需要帮助来使用Junit测试getPropertyFor()方法的值和键 public class ReadPropertyFile { private Properties properties; public ReadPropertyFile(String propertyFileName) {

我有一个类ReadPropertyFile,它有一个返回类型string的方法getPropertyFor()(这是作为参数传递的键对应的值)。我需要帮助来使用Junit测试getPropertyFor()方法的值和键

 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) {

            String value = properties.getProperty(key);
            if(value == null) {
                try {
                    throw new Exception(key + " not found!");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return value;
        }
    }
我编写了以下测试用例来测试所提供的键的值

如何测试“key”是否包含在testconfig.properties文件中? testConfig.properties的内容如下:
文件\u NAME=D:/Refreshed\u data\u daily/all\u hue\u posts\u in\u excel.xlsx

如果文件中未定义键,
getProperties
方法返回
null
。 如果密钥存在但没有值,
getProperties
方法返回一个空字符串。 在文件中捕获并抛出异常的方式没有多大意义。 您可以将方法简化为:

public String getPropertyFor(String key) {
    return properties.getProperty(key);
}
testConfig.properties
中给出此内容:

FILE_NAME = D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx
empty.test =
您可以像这样对不同的情况进行单元测试:

private String getProperty(String key) {
    new ReadPropertyFile("testConfig.properties").getPropertyFor(key)
}

@Test
public void testMissingKey() {
      assertNull(getProperty("nonexistent"));
}

@Test
public void testEmptyKey() {
      assertEquals("", getProperty("empty.prop"));
}

@Test
public void testValue() {
      assertEquals("D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx", getProperty("FILE_NAME"));
}

您已经为(键)方法编写了getPropertyFor。您是否遇到任何问题?是的,我编写了一个测试用例来检查值是否正确:@test public void testifilepathvalueexists(){ReadPropertyFile checkKey=new ReadPropertyFile(“testConfig.properties”);assertEquals(checkKey.getPropertyFor(fileNameKey),“D:/Refreshed_data_daily/all_hue_posts_in_excel.xlsx”)}如何编写用于检查键值的测试用例?您的文本文件将包含诸如key1=value1 key2=value2等条目。请检查文件中存在的键名。根据我在测试用例中的帖子,如果缺少键或值,我更喜欢首选项而不是属性,您已经编写了测试用例。如果缺少这些值中的任何一个,将引发NoSuchElementException。正确的?如何编写测试用例来检查密钥的正确性?与我为值编写的测试用例类似,只是为了测试目的,我不能添加带有null值的“empty.pro”键并对其进行测试。我想专门检查密钥“FILE\u NAME”。测试用例会是怎样的?因为这是我在程序中使用的唯一键,所以没有其他键!我的答案中的
testValue
方法就是这样做的<代码>文件名必须存在于文件中,并且必须具有特定值。如果密钥具有不同的名称或值,测试用例将失败。难道不能有单独的测试用例只检查密钥吗?类似AssertEquals(myObj.getTheKey(),“文件名”)的东西不,这样的测试没有意义。在
Properties
对象中没有“密钥”。有很多钥匙。它们有名称,如您的示例中的“文件名”。这是一个键->值对的列表。