简体   繁体   中英

How to test void methods using EasyMock

I've seen a few questions out there regarding this but I can't seem to make sense of any of the answers for my particular problem.

I have a mock object, lets call "object1", which I send to some method for testing, lets call testMethod(). So I end up calling

testMethod(object1);

for testing. Now somewhere in this testMethod, there will be a part where it calls a method

object1.toggleDisplay();

which is a void method. If the method were like

object1.getDisplay()

where it actually returns something, I usually do

EasyMock.expect(object1.getDisplay()).andReturn(whatever);

However, this is a void method, and I would like to just test that this has been indeed been called for a certain amount of times. What is the easiest way to do this?

Thanks

If things haven't changed in the last few years, you use expectLastCall when setting up your expectations.

object1.toggleDisplay();
object.expectLastCall();
object1.toggleDisplay();
EasyMock.expectLastCall().times(5);

or if you import statically the EasyMock methods:

import static org.easymock.EasyMock.*;

[...]

object1.toggleDisplay();
expectLastCall().times(5);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM