• <tfoot id='8WVt2'></tfoot>

    <i id='8WVt2'><tr id='8WVt2'><dt id='8WVt2'><q id='8WVt2'><span id='8WVt2'><b id='8WVt2'><form id='8WVt2'><ins id='8WVt2'></ins><ul id='8WVt2'></ul><sub id='8WVt2'></sub></form><legend id='8WVt2'></legend><bdo id='8WVt2'><pre id='8WVt2'><center id='8WVt2'></center></pre></bdo></b><th id='8WVt2'></th></span></q></dt></tr></i><div id='8WVt2'><tfoot id='8WVt2'></tfoot><dl id='8WVt2'><fieldset id='8WVt2'></fieldset></dl></div>
      <bdo id='8WVt2'></bdo><ul id='8WVt2'></ul>
    1. <small id='8WVt2'></small><noframes id='8WVt2'>

      <legend id='8WVt2'><style id='8WVt2'><dir id='8WVt2'><q id='8WVt2'></q></dir></style></legend>

        mockito 有与 jMock 的 States 等效的成语吗?

        时间:2023-09-25

          • <i id='wPnjZ'><tr id='wPnjZ'><dt id='wPnjZ'><q id='wPnjZ'><span id='wPnjZ'><b id='wPnjZ'><form id='wPnjZ'><ins id='wPnjZ'></ins><ul id='wPnjZ'></ul><sub id='wPnjZ'></sub></form><legend id='wPnjZ'></legend><bdo id='wPnjZ'><pre id='wPnjZ'><center id='wPnjZ'></center></pre></bdo></b><th id='wPnjZ'></th></span></q></dt></tr></i><div id='wPnjZ'><tfoot id='wPnjZ'></tfoot><dl id='wPnjZ'><fieldset id='wPnjZ'></fieldset></dl></div>

            <small id='wPnjZ'></small><noframes id='wPnjZ'>

              <tbody id='wPnjZ'></tbody>
            <legend id='wPnjZ'><style id='wPnjZ'><dir id='wPnjZ'><q id='wPnjZ'></q></dir></style></legend>

              • <bdo id='wPnjZ'></bdo><ul id='wPnjZ'></ul>

                1. <tfoot id='wPnjZ'></tfoot>
                2. 本文介绍了mockito 有与 jMock 的 States 等效的成语吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  Growing Object Oriented Software 一书在 jMock 中提供了几个示例,其中状态是显式的,而不通过 API 公开.我真的喜欢这个主意.有没有办法在 Mockito 中做到这一点?

                  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?

                  这是书中的一个例子

                  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 a spy for the self same exercise:

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

                  因此,我将 SniperListener 模拟更改为间谍:

                  I changed my SniperListener mock into a spy thus:

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

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

                  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() {
                      }
                  }
                  

                  本书使用了 JMock 的States",但我使用了嵌套枚举:

                  The book uses JMock's "States", but I used a nested enum instead:

                  private SniperState sniperState = SniperState.idle;
                  
                  private enum SniperState {
                      idle, winning, bidding
                  }
                  

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

                  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);
                  }
                  

                  这篇关于mockito 有与 jMock 的 States 等效的成语吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Java中的Mockito“在此处检测到错误的参数" 下一篇:在 AndroidTestCase 中调用 MockitoAnnotations.initMocks() 时

                  相关文章

                  最新文章

                  <i id='TxEhk'><tr id='TxEhk'><dt id='TxEhk'><q id='TxEhk'><span id='TxEhk'><b id='TxEhk'><form id='TxEhk'><ins id='TxEhk'></ins><ul id='TxEhk'></ul><sub id='TxEhk'></sub></form><legend id='TxEhk'></legend><bdo id='TxEhk'><pre id='TxEhk'><center id='TxEhk'></center></pre></bdo></b><th id='TxEhk'></th></span></q></dt></tr></i><div id='TxEhk'><tfoot id='TxEhk'></tfoot><dl id='TxEhk'><fieldset id='TxEhk'></fieldset></dl></div>

                  <tfoot id='TxEhk'></tfoot>

                    • <bdo id='TxEhk'></bdo><ul id='TxEhk'></ul>

                  1. <legend id='TxEhk'><style id='TxEhk'><dir id='TxEhk'><q id='TxEhk'></q></dir></style></legend>

                      <small id='TxEhk'></small><noframes id='TxEhk'>