Use Mockito to verify that nothing is called after a method

后端 未结 3 1779
小鲜肉
小鲜肉 2020-12-25 11:04

I\'m using Mockito to write a unit test in Java, and I\'d like to verify that a certain method is the last one called on an object.

I\'m doing something lik

3条回答
  •  再見小時候
    2020-12-25 11:22

    I think it requires more custom work.

    verify(row, new LastCall()).saveToDatabase();
    

    and then

    public class LastCall implements VerificationMode {
        public void verify(VerificationData data) {
            List invocations = data.getAllInvocations();
            InvocationMatcher matcher = data.getWanted();
            Invocation invocation = invocations.get(invocations.size() - 1);
            if (!matcher.matches(invocation)) throw new MockitoException("...");
        }
    }
    

    Previous Answer:

    You are right. verifyNoMoreInteractions is what you need.

    verify(row).setSomething(value);
    verify(row).setSomethingElse(anotherValue);
    verify(row).editABunchMoreStuff();
    verify(row).saveToDatabase();
    verifyNoMoreInteractions(row);
    

提交回复
热议问题