mockito - 伪造 addObserver
我从mockito开始,想知道如何假装添加观察者。我想编写一个测试来确保观察者计数在函数调用后增加。
示例测试代码:
MyClassUnderTest instance = new MyClassUnderTest();
AudioDeviceManager adm = mock(AudioDeviceManager.class);
assertEquals(adm.countObservers(), 0);
instance.setup(adm, microphone);
//Inside the setup function, microphone is added as an observer
//to the device manager: adm.addObserver(microphone);
assertEquals(adm.countObservers(), 1);
由于 adm 是一个模拟,我知道我必须定义 addObserver
的逻辑,但我不知道该做什么 - when(adm.addObserver(Observer o)).then(?)
I am beginning with mockito and wondering how to fake adding an observer. I want to write a test that ensures that the observer count has increased after a function call.
example testing code:
MyClassUnderTest instance = new MyClassUnderTest();
AudioDeviceManager adm = mock(AudioDeviceManager.class);
assertEquals(adm.countObservers(), 0);
instance.setup(adm, microphone);
//Inside the setup function, microphone is added as an observer
//to the device manager: adm.addObserver(microphone);
assertEquals(adm.countObservers(), 1);
Since adm is a mock, I know I have to define the logic of addObserver
but I do not know what to -when(adm.addObserver(Observer o)).then(?)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
布莱恩,
使用验证。例如,运行
并检查 http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html
干杯,
一个。
brian,
use verify. For example instead of the assert, run
and check the first chapter of http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html
Cheers,
a.
如果您正在测试 MyClassUnderTest 那么您不应该关心 adm 的作用。为 AudioDeviceManager 编写一组单独的测试用例,其中未对其进行模拟。
If you are testing MyClassUnderTest then you shouldn't be caring what adm does. Write a separate set of test cases for AudioDeviceManager where it isn't mocked.