java Mocki验证方法调用的顺序

332nm8kg  于 2023-06-28  发布在  Java
关注(0)|答案(6)|浏览(118)

有没有办法验证在Mockito中是否在methodTwo之前调用了methodOne

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();
    }
}
c2e8gylq

c2e8gylq1#

InOrder可以帮助你做到这一点。

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();
g2ieeal7

g2ieeal72#

请注意,您还可以使用InOrder类来验证是否在单个模拟上按顺序调用了各种方法,而不仅仅是在两个或多个模拟上。
假设我有两个类FooBar

public class Foo {
  public void first() {}
  public void second() {}
}

public class Bar {
  public void firstThenSecond(Foo foo) {
    foo.first();
    foo.second();
  }
}

然后,我可以添加一个测试类来测试BarfirstThenSecond()方法实际上调用了first(),然后调用了second(),而不是second(),然后调用了first()。请参见以下测试代码:

public class BarTest {
  @Test
  public void testFirstThenSecond() {
    Bar bar = new Bar();
    Foo mockFoo = Mockito.mock(Foo.class);
    bar.firstThenSecond(mockFoo);

    InOrder orderVerifier = Mockito.inOrder(mockFoo);
    // These lines will PASS
    orderVerifier.verify(mockFoo).first();
    orderVerifier.verify(mockFoo).second();

    // These lines will FAIL
    // orderVerifier.verify(mockFoo).second();
    // orderVerifier.verify(mockFoo).first();
  }
}
eni9jsuy

eni9jsuy3#

是的,文档中对此进行了描述。你必须使用InOrder类。
示例(假设已经创建了两个模拟):

InOrder inOrder = inOrder(serviceAMock, serviceBMock);

inOrder.verify(serviceAMock).methodOne();
inOrder.verify(serviceBMock).methodTwo();
5jvtdoz2

5jvtdoz24#

对于Kotlin用户,你可以这样做:

class MyTrackerTest {
    private val trackEventUseCase: TrackEventUseCase = mock()
    private val sut = MyTracker(trackEventUseCase)

    @Test
    fun `trackSomething SHOULD invoke tracker use case twice with correct event names WHEN called`() {
        sut.trackSomething()

        trackEventUseCase.inOrder {
            verify().invoke("Is it August?")
            verify().invoke("No!")
        }
    }

}

ijxebb2r

ijxebb2r5#

对于BDD来说

@Test
public void testOrderWithBDD() {

    // Given
    ServiceClassA firstMock = mock(ServiceClassA.class);
    ServiceClassB secondMock = mock(ServiceClassB.class);

    //create inOrder object passing any mocks that need to be verified in order
    InOrder inOrder = inOrder(firstMock, secondMock);

    willDoNothing().given(firstMock).methodOne();
    willDoNothing().given(secondMock).methodTwo();

    // When
    firstMock.methodOne();
    secondMock.methodTwo();

    // Then
    then(firstMock).should(inOrder).methodOne();
    then(secondMock).should(inOrder).methodTwo();

}
pdsfdshx

pdsfdshx6#

当验证具有不同参数的单个方法调用的调用顺序时,本质上是相同的:

ServiceClass myMock = mock(ServiceClassA.class);

InOrder inOrder = inOrder(myMock);

inOrder.when(myMock).myMethod(42);
inOrder.when(myMock).myMethod(69);

相关问题