我正在创建一个单元测试,以显示两个方法并行执行。为此,我模拟了两种方法,让它们都有2秒的延迟。然后我验证单元测试花费少于4(所以我可以确定动作不是按顺序执行的,因为它需要超过4 [2 * 2]秒)。
有更好的方法吗?
答案 0 :(得分:1)
我会使用InOrder功能。 这是两个方法来自同一个mock的情况的一个例子:
bar
您也可以将InOrder与多个模拟一起使用:
false
在这个例子中,mock3只是一个实例,其唯一目的是挂钩两个方法的执行结束,因此应该使用@Test
public void foo() {
MyClass mock = Mockito.mock(MyClass.class);
Mockito.when(mock.methodOne())thenAnswer(new Answer<ReturnType>()
{
@Override
public ReturnType answer(InvocationOnMock invocation) throws Throwable
{
// Implement the delay or parallel execution
. . .
mock.methodThree(); // This invocation is just a hook to intercept the end of this method execution
return something;
}
});
Mockito.when(mock.methodTwo())thenAnswer(new Answer<ReturnType>()
{
@Override
public ReturnType answer(InvocationOnMock invocation) throws Throwable
{
// Implement the delay or the parallel execution
. . .
mock.methodThree(); // This invocation is just a hook to intercept the end of this method execution
return something;
}
});
// Here there should be the call to the real method that calls the two methods in parallel:
// !!HERE!!
// mock1.methodOne();
// mock2.methodTwo();
InOrder inOrder = Mockito.inOrder(mock1, mock2);
inOrder.verify(mock1).methodOne(); //asserts that methodOne should be invoked first
inOrder.verify(mock2).methodTwo(); //asserts that methodTwo should be invoked after methodOne
inOrder.verify(mock3, Mockito.calls(2)).methodThree(); //asserts that methodThree, that is invoked at the end of methodOne, is invoked after the methodTwo invocation. These asserts together tell us that methodTwo was called during the execution of methodOne.
}
进行模拟。也许在你的场景中,这可以有所不同。
编辑:
我现在将更好地解释我的答案:在我分享的例子中,只有嘲笑,所以测试没用。在我添加@Test
public void foo() {
MyClass mock1 = Mockito.mock(MyClass.class);
MyClass mock2 = Mockito.mock(MyClass.class);
OtherClass mock3 = Mockito.mock(OtherClass.class);
Mockito.when(mock1.methodOne())thenAnswer(new Answer<ReturnType>()
{
@Override
public ReturnType answer(InvocationOnMock invocation) throws Throwable
{
// Implement the delay or the parallel execution
. . .
mock3.methodThree(); // This invocation is just a hook to intercept the end of this method execution
return something;
}
});
Mockito.when(mock2.methodTwo())thenAnswer(new Answer<ReturnType>()
{
@Override
public ReturnType answer(InvocationOnMock invocation) throws Throwable
{
// Implement the delay or the parallel execution
. . .
mock3.methodThree(); // This invocation is just a hook to intercept the end of this method execution
return something;
}
});
// Here there should be the call to the real method that calls the two methods in parallel:
// !!HERE!!
// mock1.methodOne();
// mock2.methodTwo();
InOrder inOrder = Mockito.inOrder(mock1, mock2);
inOrder.verify(mock1).methodOne(); //asserts that methodOne should be invoked first
inOrder.verify(mock2).methodTwo(); //asserts that methodTwo should be invoked after methodOne
inOrder.verify(mock3, Mockito.calls(2)).methodThree(); //asserts that methodThree, that is invoked at the end of methodOne, is invoked after the methodTwo invocation. These asserts together tell us that methodTwo was called during the execution of methodOne.
}
的代码中,应该调用实际上并行调用两个模拟方法的实际方法。或者,应该实现doNothing
的两个实例并行执行,但仅使用mocks的测试没有用。
鉴于此,inOrder配置为我在我的示例中所做的,验证第二次调用是在第一次调用结束之前发生的(查看我添加的注释)。
有关InOrder界面的更多信息: http://site.mockito.org/mockito/docs/current/org/mockito/InOrder.html http://www.tutorialspoint.com/mockito/mockito_ordered_verification.htm