【问题标题】:How to test only the "key" from .properties file in java using Junit如何使用 Junit 在 java 中仅测试 .properties 文件中的“密钥”
【发布时间】:2015-10-11 04:40:51
【问题描述】:

我有一个 ReadPropertyFile 类,其中包含名为 getPropertyFor 的方法,该方法返回给定键的值作为参数。我从其他类调用的 getPropertyFor() 方法,使用键作为输入参数。我的属性 filetestConfig.properties(file name) 的内容是: 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");
}

上面的测试用例是检查函数返回的值是否正确。如何检查密钥是否正确?什么是可能的测试用例来测试关键。而且我不想出于测试目的更改我的 testConfig.properties 文件。 我需要帮助,我是 Java Junit 测试的新手。

【问题讨论】:

  • 您究竟要检查什么密钥?密钥是否存在于属性文件中?或者你想根据值找到键?
  • 我想检查属性文件中是否存在密钥。如何检查?
  • 只做 assertNotNull(properties.getProperty(FILE_NAME)
  • 这个测试实际上会检查键 FILE_NAME 对应的值是否不为空!

标签: java junit key properties-file


【解决方案1】:

假设您有一个名为 myprop.properties 的属性文件:

key1=value1
key2=
key3

您可以编写以下 4 个测试来检查属性是否丢失。如不同情况所示,只有不在属性文件中的键 (key4) 会在您调用 properties.get("key4") 时返回 null。在 key1 的情况下,它返回 value1。 key2 和 key3 的情况下,返回空字符串。

因此,要检查属性文件中是否存在键,您可以使用 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"));
}
}

【讨论】:

    【解决方案2】:

    如果要测试属性文件中是否存在键,有两种方法:

    如果已通过属性构造函数为您的属性指定了一些预定义值的默认值,并且如果您尝试获取该键但它不存在,您将取回此默认值。在这种情况下,您必须检查您的回报是否等于此默认值。

    但是如果你没有提供默认值,那么你可以简单地在它的返回值上使用 asserNotNull,因为 javadoc for Propery#getProperty:

    在此属性列表中搜索具有指定键的属性。如果在该属性列表中未找到该键,则递归地检查默认属性列表及其默认值。如果未找到该属性,则该方法返回 null。

    但在单元测试的情况下,您必须提供许多不同的属性来测试您的方法或类的所有可能行为。

    【讨论】:

    • 'asserNotNull(propertyFileObj.getPropertyFor(keyForFilePath))' 是检查密钥是否存在的有效测试用例吗?或者这个测试用例检查键 FILE_NAME 对应的值是否不为空! ?我在这里感到困惑!
    • 更新了答案。如果您没有提供默认值,asserNotNull 是有效的。如果您提供了它们,则必须从属性实例中检查它们,您的返回值是否等于默认值。但无论如何,你必须只检查你的类行为,如果它有一些特殊的逻辑以防万一,你没有提供密钥,那么你必须测试它。如果你不这样做,就没有理由这样做。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-26
    • 1970-01-01
    • 2018-02-14
    • 1970-01-01
    • 2011-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多