本文介紹了Mockito:如何存根 getter setter的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我是 Mockito 的新手,我想知道如何存根獲取/設置對.
I am kind of new to Mockito and I was wondering how I could stub a get/set pair.
例如
public interface Dummy {
public String getString();
public void setString(String string);
}
如何使它們正常運行:如果在測試的某個地方調用 setString("something");
我希望 getString()
返回something".這是可行的還是有更好的方法來處理這種情況?
How can I make them behave properly: if somewhere in a test I invoke setString("something");
I would like getString()
to return "something". Is that feasable or is there a better way to handle such cases?
推薦答案
我還希望 getter 返回最近 setter 調用的結果.
I also wanted the getter to return the result of the recent setter-call.
擁有
class Dog
{
private Sound sound;
public Sound getSound() {
return sound;
}
public void setSound(Sound sound) {
this.sound = sound;
}
}
class Sound
{
private String syllable;
Sound(String syllable) {
this.syllable = syllable;
}
}
我使用以下方法將 setter 連接到 getter:
I used the following to connect the setter to the getter:
final Dog mockedDog = Mockito.mock(Dog.class, Mockito.RETURNS_DEEP_STUBS);
// connect getter and setter
Mockito.when(mockedDog.getSound()).thenCallRealMethod();
Mockito.doCallRealMethod().when(mockedDog).setSound(Mockito.any(Sound.class));
這篇關于Mockito:如何存根 getter setter的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!