EasyMock andReturn() vs andStubReturn()

后端 未结 2 1882
一个人的身影
一个人的身影 2020-12-13 23:34

What is the difference between using andReturn(T value) vs andStubReturn(T value) for EasyMock?

In what situation would you use andSt

2条回答
  •  心在旅途
    2020-12-14 00:01

    An additional note for clarity.

    If you use .andStubReturn() (or if you use .andReturn(foo).anyTimes()), there will be no minimum expected call count. So if you set a mock expectation using either of these two, and the mocked method is NOT called, the .verify() call will not assert.

    Example that will NOT assert when the mocked method isn't called;

    FooClass myFooClass = EasyMock.createNiceMock(FooClass.class);
    EasyMock.expect(myFooClass.someMethod(EasyMock.anyInt()).andStubReturn(true);
    EasyMock.replay(myFooClass);
    
    EasyMock.verify(myFooClass);
    

    Example that WILL assert when the mocked method isn't called;

    FooClass myFooClass = EasyMock.createNiceMock(FooClass.class);
    EasyMock.expect(myFooClass.someMethod(EasyMock.anyInt()).andReturn(true).atLeastOnce();
    EasyMock.replay(myFooClass);
    
    EasyMock.verify(myFooClass);
    

提交回复
热议问题