【问题标题】:Equivalent of times() in JMockIt?相当于 JMockIt 中的 times()?
【发布时间】:2011-10-09 02:00:36
【问题描述】:

我不认为 minInvocation 或 maxInvocation 等同于 Mockito 中的 times()。有吗?

请看这个问题:Major difference between: Mockito and JMockIt

还没有人回答。

编辑

我自己找到了答案:在此处添加给需要此答案的其他人:

解决方案是使用 DynamicPartialMocking 并将对象传递给 Expectations 或 NonStrictExpectations 的构造函数,而不是调用该对象上的任何函数。

然后在 Verifications 部分中,调用您要测量其调用次数的对象上的任何函数并设置 times = 您想要的值

new NonStrictExpectations(Foo.class, Bar.class, zooObj)
{
    {
        // don't call zooObj.method1() here
        // Otherwise it will get stubbed out
    }
};


new Verifications()
{
    {
        zooObj.method1(); times = N;
    }
};

【问题讨论】:

  • 您能否将答案添加为答案(并接受它)——而不是对问题进行编辑?这样,它就不会显示在“未答复”存储桶中?

标签: jmockit


【解决方案1】:

我自己找到了答案:在此处添加给需要此答案的其他人:

解决方案是使用 DynamicPartialMocking 并将对象传递给 Expectations 或 NonStrictExpectations 的构造函数,而不是调用该对象上的任何函数。

然后在 Verifications 部分中,调用您要测量其调用次数的对象上的任何函数并设置 times = 您想要的值

new NonStrictExpectations(Foo.class, Bar.class, zooObj)
{
    {
        // don't call zooObj.method1() here
        // Otherwise it will get stubbed out
    }
};


new Verifications()
{
    {
        zooObj.method1(); times = N;
    }
};

【讨论】:

  • 在大多数情况下,您不需要使用部分模拟。相反,通过使用@Mocked@NonStrict 等模拟注释之一声明模拟字段或模拟参数来使用“常规”模拟。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-10-11
  • 2013-06-11
  • 1970-01-01
  • 2013-07-18
  • 1970-01-01
  • 2011-11-08
  • 2021-09-14
相关资源
最近更新 更多