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”,你就可以直接得到对应的值。 在这种情况下,您不需要使用期望值