Mockito: how to stub getter setter(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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Mockito:如何存根 getter setter


基础教程推荐
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01