java - Does mockito have an equivalent idiom to jMock's States? -
the book growing object oriented software gives several examples in jmock state made explicit without exposing through api. idea. there way in mockito?
here's 1 example book
public class sniperlaunchertest { private final states auctionstate = context.states("auction state") .startsas("not joined"); @test public void addsnewsnipertocollectorandthenjoinsauction() { final string itemid = "item 123"; context.checking(new expectations() {{ allowing(auctionhouse).auctionfor(itemid); will(returnvalue(auction)); oneof(snipercollector).addsniper(with(sniperforitem(item))); when(auctionstate.is("not joined")); oneof(auction).addauctioneventlistener(with(sniperforitem(itemid))); when(auctionstate.is("not joined")); one(auction).join(); then(auctionstate.is("joined")); }}); launcher.joinauction(itemid); } }
i used spy self same exercise:
http://docs.mockito.googlecode.com/hg/latest/org/mockito/mockito.html#13
i changed sniperlistener mock spy thus:
private final sniperlistener sniperlistenerspy = spy(new sniperlistenerstub()); private final auctionsniper sniper = new auctionsniper(auction, sniperlistenerspy); and created stubbed implementation of sniperlistener:
private class sniperlistenerstub implements sniperlistener { @override public void sniperlost() { } @override public void sniperbidding() { sniperstate = sniperstate.bidding; } @override public void sniperwinning() { } } the book uses jmock's "states", used nested enum instead:
private sniperstate sniperstate = sniperstate.idle; private enum sniperstate { idle, winning, bidding } you have use regular junit asserts test state:
@test public void reportslostifauctioncloseswhenbidding() { sniper.currentprice(123, 45, pricesource.fromotherbidder); sniper.auctionclosed(); verify(sniperlistenerspy, atleastonce()).sniperlost(); assertequals(sniperstate.bidding, sniperstate); }
Comments
Post a Comment