Mockito验证方法调用的顺序/顺序 [英] Mockito verify order / sequence of method calls
本文介绍了Mockito验证方法调用的顺序/顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法验证在Mockito中 methodTwo
之前是否调用 methodOne
?
Is there a way to verify if a methodOne
is called before methodTwo
in Mockito?
public class ServiceClassA {
public void methodOne(){}
}
public class ServiceClassB {
public void methodTwo(){}
}
public class TestClass {
public void method(){
ServiceClassA serviceA = new ServiceClassA();
ServiceClassB serviceB = new ServiceClassB();
serviceA.methodOne();
serviceB.methodTwo();
}
}
推荐答案
< a href =http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#inOrder(java.lang.Object ...)> InOrder
可以帮助你做到这一点。
InOrder
helps you to do that.
ServiceClassA firstMock = mock(ServiceClassA.class);
ServiceClassB secondMock = mock(ServiceClassB.class);
Mockito.doNothing().when(firstMock).methodOne();
Mockito.doNothing().when(secondMock).methodTwo();
//create inOrder object passing any mocks that need to be verified in order
InOrder inOrder = inOrder(firstMock, secondMock);
//following will make sure that firstMock was called before secondMock
inOrder.verify(firstMock).methodOne();
inOrder.verify(secondMock).methodTwo();
这篇关于Mockito验证方法调用的顺序/顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文