我有一个类,其中有一个我想测试的方法,但它调用同一类中的私有方法来获取 Map 值。我想模拟私有方法返回给我想要测试的方法的内容。
import java.util.*;
public class ClassOne {
public List<String> getList(String exampleString) {
List<String> exampleList = null;
Map<String, String> exampleMap = getMap();
String exampleValue = exampleMap.get(exampleString);
// Does some stuff
exampleList = Arrays.asList(exampleValue.split(","));
return exampleList;
}
private Map<String, String> getMap(){
Map<String, String> exampleMap = new HashMap<>();
exampleMap.put("pre-defined String", "pre-defined String");
return exampleMap;
}
}
Run Code Online (Sandbox Code Playgroud)
据我所知,我似乎想使用 PowerMock,但我似乎无法弄清楚。
一种在 getList 方法调用私有方法 getMap() 时模拟其返回的方法。