Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing Jmockit在单元测试(testng)上获取空指针异常一个getter方法_Unit Testing_Nullpointerexception_Testng_Jmockit - Fatal编程技术网

Unit testing Jmockit在单元测试(testng)上获取空指针异常一个getter方法

Unit testing Jmockit在单元测试(testng)上获取空指针异常一个getter方法,unit-testing,nullpointerexception,testng,jmockit,Unit Testing,Nullpointerexception,Testng,Jmockit,我有以下代码 //Unit to be tested (classname : PropertiesFile) public static String getProperty(String name) { return propertiesMap.get(name).toString(); } 其中propertiesMap是定义为 地图属性地图 以下是我的TestNG测试: @Test() public void testGetProperty1(@Mocked f

我有以下代码

//Unit to be tested (classname : PropertiesFile)
public static String getProperty(String name) {
        return propertiesMap.get(name).toString();
    }
其中propertiesMap是定义为 地图属性地图

以下是我的TestNG测试:

@Test()
public void testGetProperty1(@Mocked final Map<String, String> propertiesMap)

{
    new NonStrictExpectations()
    {{
            propertiesMap.get("region");
            result = anyString;
        }};

    PropertiesFile.getProperty("region");

}
@Test()
public void testGetProperty1(@Mocked final Map propertiesMap)
{
新的非严格测量()
{{
属性映射获取(“区域”);
结果=任意字符串;
}};
PropertiesFile.getProperty(“区域”);
}
我在返回线上得到NPE


请让我知道我做错了什么?以及如何摆脱这种NPE,并使测试正常工作。

到目前为止,我还没有模拟集合,所以不知道它们是否可以被模拟。 此外,如果你想使用它,只需在propertiesMap中设置属性,键为“region”,你就可以直接得到对应的值。 在这种情况下,您不需要使用期望值