mockito - faking addObserver
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();
A开发者_开发百科udioDeviceManager 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(?)
brian,
use verify. For example instead of the assert, run
verify(adm).countObservers( AnyObject)
and check the first chapter of http://mockito.googlecode.com/svn/branches/1.5/javadoc/org/mockito/Mockito.html
Cheers, a.
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.
精彩评论