mockito 是否有与 jMock 的 States 等效的习惯用法?
《Growing Object Oriented Software》一书给出了 jMock 中的几个示例,其中状态是明确的,而不通过 API 公开。我真的很喜欢这个主意。 Mockito 有办法做到这一点吗?
这是书中的一个例子
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);
}
}
The book Growing Object Oriented Software gives several examples in jMock where the state is made explicit without exposing it through an API. I really like this idea. Is there a way to do this in Mockito?
Here's one example from the 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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我使用间谍进行相同的练习:
http:// /docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#13
我将我的 SniperListener 模拟更改为间谍:
并且还创建了一个存根实现SniperListener:
这本书使用 JMock 的“状态”,但我使用了嵌套枚举:
然后您必须使用常规 JUnit 断言来测试状态:
I used a spy for the self same exercise:
http://docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#13
I changed my SniperListener mock into a spy thus:
And also created a stubbed implementation of SniperListener:
The book uses JMock's "States", but I used a nested enum instead:
You then have to use regular JUnit asserts to test for the state:
据我所知。我已经大量使用了mockito,并且 doco 中没有任何内容与我在 JMock 网站上读到的有关状态的内容类似。如果我正确的话,它们基本上将发生异常的时间限制为另一个对象的特定状态的持续时间。这是一个有趣的想法,但我很难看到它的应用程序。
在 Mockito 中,您可以使用带回调的存根来执行代码做同样的工作。在回调方法中,您可以执行进一步的状态验证。或者,您可以按原样使用 自定义参数匹配器也在调用时执行。
这两种方法都可以让您在执行时访问代码,也就是您想要检查状态的时间。
Not that I'm aware of. I've used mockito a far amount and there's nothing in the doco similar to what I read on the JMock site about states. If I have it correctly they basically limit the time at which an exepection can occur to the duration of a specific state of another object. It's an interesting idea, but I'm struggling to see the applications for it.
In Mockito you can execute code using Stubbing with callbacks to do the same job. In the callback method you can execute further validations of the state. Alternatively you can employ a Custom argument matcher as they are also executed at the time of the call.
Both of these give you access to the code at execution time which is the time you want to check the state.