Mockito:如何存根 getter setter

编程入门 行业动态 更新时间:2024-10-20 13:43:08
本文介绍了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

本文发布于:2023-10-31 04:45:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545043.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:存根   Mockito   setter   getter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!