mockito 是否有与 jMock 的 States 等效的习惯用法?

发布于 2024-11-18 19:10:17 字数 971 浏览 5 评论 0原文

《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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不及他 2024-11-25 19:10:17

我使用间谍进行相同的练习:

http:// /docs.mockito.googlecode.com/hg/latest/org/mockito/Mockito.html#13

我将我的 SniperListener 模拟更改为间谍:

private final SniperListener sniperListenerSpy = spy(new SniperListenerStub());
private final AuctionSniper sniper = new AuctionSniper(auction, sniperListenerSpy);

并且还创建了一个存根实现SniperListener:

private class SniperListenerStub implements SniperListener {
    @Override
    public void sniperLost() {
    }

    @Override
    public void sniperBidding() {
        sniperState = SniperState.bidding;
    }

    @Override
    public void sniperWinning() {
    }
}

这本书使用 JMock 的“状态”,但我使用了嵌套枚举:

private SniperState sniperState = SniperState.idle;

private enum SniperState {
    idle, winning, bidding
}

然后您必须使用常规 JUnit 断言来测试状态:

@Test
public void reportsLostIfAuctionClosesWhenBidding() {
    sniper.currentPrice(123, 45, PriceSource.FromOtherBidder);
    sniper.auctionClosed();
    verify(sniperListenerSpy, atLeastOnce()).sniperLost();
    assertEquals(SniperState.bidding, sniperState);
}

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:

private final SniperListener sniperListenerSpy = spy(new SniperListenerStub());
private final AuctionSniper sniper = new AuctionSniper(auction, sniperListenerSpy);

And also created a 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", but I used a nested enum instead:

private SniperState sniperState = SniperState.idle;

private enum SniperState {
    idle, winning, bidding
}

You then have to use regular JUnit asserts to test for the state:

@Test
public void reportsLostIfAuctionClosesWhenBidding() {
    sniper.currentPrice(123, 45, PriceSource.FromOtherBidder);
    sniper.auctionClosed();
    verify(sniperListenerSpy, atLeastOnce()).sniperLost();
    assertEquals(SniperState.bidding, sniperState);
}
话少情深 2024-11-25 19:10:17

据我所知。我已经大量使用了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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文