java – mockito是否具有与jMock的国家相同的成语?

前端之家收集整理的这篇文章主要介绍了java – mockito是否具有与jMock的国家相同的成语?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
“面向对象软件”这本书在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);
   }
}

解决方法

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

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的一个stubbed实现:

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

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

    @Override
    public void sniperWinning() {
    }
}

这本书使用JMock的“States”,但是我使用了一个嵌套的枚举:

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);
}
原文链接:https://www.f2er.com/java/121658.html

猜你在找的Java相关文章