@Nullable private Expectation findPreviousNotStrictExpectation(@Nonnull ExpectedInvocation newInvocation) { int n = notStrictExpectations.size(); if (n == 0) { return null; } Object mock = newInvocation.instance; String mockClassDesc = newInvocation.getClassDesc(); String mockNameAndDesc = newInvocation.getMethodNameAndDescription(); boolean constructorInvocation = newInvocation.isConstructor(); for (int i = 0; i < n; i++) { Expectation previousExpectation = notStrictExpectations.get(i); if (isMatchingInvocation( mock, mockClassDesc, mockNameAndDesc, constructorInvocation, previousExpectation) && isWithMatchingArguments(newInvocation, previousExpectation.invocation)) { return previousExpectation; } } return null; }
private boolean isMatchingInvocation( @Nullable Object mock, @Nonnull String mockClassDesc, @Nonnull String mockNameAndDesc, boolean constructorInvocation, @Nonnull Expectation expectation) { ExpectedInvocation invocation = expectation.invocation; return invocation.isMatch(mockClassDesc, mockNameAndDesc) && isSameMockedClass(mock, invocation.instance) && (constructorInvocation || mock == null || isMatchingInstance(mock, expectation)); }
private void removeMatchingExpectationsCreatedBefore(@Nonnull ExpectedInvocation invocation) { Expectation previousExpectation = findPreviousNotStrictExpectation(invocation); if (previousExpectation != null) { notStrictExpectations.remove(previousExpectation); invocation.copyDefaultReturnValue(previousExpectation.invocation); } }
private void forceMatchingOnMockInstanceIfRequired(@Nonnull ExpectedInvocation invocation) { if (isToBeMatchedOnInstance(invocation.instance, invocation.getMethodNameAndDescription())) { invocation.matchInstance = true; } }