【问题标题】:How to verify a method is called at most n times to work with InOrder of Mockito如何验证方法最多调用 n 次以使用 Mockito 的 InOrder
【发布时间】:2017-10-31 01:07:03
【问题描述】:

我想验证getCurrentPrice() 最多 15 次。正如您在我的代码中看到的那样,我尝试使用atMost(),但是当我运行它时,它显示AtMost 未实现与InOrder 一起使用。如何解决这个问题?

@Override
public boolean validate() 
{
    String username ="Amy";
    String password = "345";
    verify(delegate,times(1)).login(username, password);
    verify(delegate,atMost(15)).getCurrentPrice(any(Cloth.class));
    InOrder protocol  = inOrder(delegate);
    protocol.verify(delegate,times(1)).login(username, password);
    protocol.verify(delegate,atMost(15)).getCurrentPrice(any(Cloth.class));     

    return true;
}

【问题讨论】:

    标签: mockito inorder


    【解决方案1】:

    您已经验证 getCurrentPrice 最多被调用了 15 次,因此在测试调用顺序时,您只需验证 login 是否在任何 getCurrentPrice 方法被调用之前被调用:

    protocol.verify(delegate).getCurrentPrice(any(Cloth.class)); 
    

    【讨论】:

    • 这并没有解决它,因为它仍然说想要 1 次但是是 15 次
    猜你喜欢
    • 2013-01-31
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    相关资源
    最近更新 更多