我想模擬外部API調用,但與代碼結構我不知道mockito是否會幫助。使用mockito模擬基類中的方法或模擬靜態方法
我有一個SimpleController:
public class SimpleController extends Anothercontroller
{
@RequestMapping("/classA")
{
.......
String response = postCall(url, .....);
}
}
public class AnotherController
{
public String postCall (String url, ......)
{
//This is the apache library to make post calls
return WebUtil.post(......);
}
}
所以現在我需要模擬postCall這是外部服務的調用。
在這裏,我可以在2個地方嘲笑:
1)postCall()在SimpleController,howevere我不知道該怎麼做,因爲它有利於繼承了組成。
2)WebUtil.post(.....)但是我不知道mockito如何模擬靜態方法。
我不想重構代碼結構,因爲還有很多其他代碼依賴於它。