![](/img/trans.png)
[英]How to mock the return of a public method called in a private method inSpring
[英]How to mock a return from a call to a private method
我有一個 class,它有一個我想測試的方法,但它在同一個 class 中調用一個私有方法來獲得 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;
}
}
據我所知,我似乎想使用 PowerMock,但我似乎無法弄清楚。
一種在方法 getList 調用時模擬私有方法 getMap() 返回的方法。
你應該能夠使用 powermock 來模擬它
“使用 PowerMock 模擬私有方法 | Baeldung” https://www.baeldung.com/powermock-private-method
您將能夠返回來自私有方法的調用,前提是它位於私有方法所在的同一 class 中
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.