private List<Object> selectMatchingName(Collection<Object> mocks, Field candidateFieldToBeInjected) { List<Object> mockNameMatches = new ArrayList<Object>(); for (Object mock : mocks) { if (candidateFieldToBeInjected.getName().equals(getMockName(mock).toString())) { mockNameMatches.add(mock); } } return mockNameMatches; }
public static MockitoException delegatedMethodHasWrongReturnType(Method mockMethod, Method delegateMethod, Object mock, Object delegate) { return new MockitoException(join( "Methods called on delegated instance must have compatible return types with the mock.", "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock), "return type should be: " + mockMethod.getReturnType().getSimpleName() + ", but was: " + delegateMethod.getReturnType().getSimpleName(), "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")" )); }
private boolean anotherCandidateMatchesMockName(final Collection<Object> mocks, final Field candidateFieldToBeInjected, final List<Field> allRemainingCandidateFields) { String mockName = getMockName(mocks.iterator().next()).toString(); for (Field otherCandidateField : allRemainingCandidateFields) { if (!otherCandidateField.equals(candidateFieldToBeInjected) && otherCandidateField.getType().equals(candidateFieldToBeInjected.getType()) && otherCandidateField.getName().equals(mockName)) { return true; } } return false; } }
public static MockitoException delegatedMethodDoesNotExistOnDelegate(Method mockMethod, Object mock, Object delegate) { return new MockitoException(join( "Methods called on mock must exist in delegated instance.", "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock), "no such method was found.", "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")" )); }
public OngoingInjecter filterCandidate(Collection<Object> mocks, Field field, Object fieldInstance) { List<Object> mockNameMatches = new ArrayList<Object>(); if(mocks.size() > 1) { for (Object mock : mocks) { if (field.getName().equals(mockUtil.getMockName(mock).toString())) { mockNameMatches.add(mock); } } return next.filterCandidate(mockNameMatches, field, fieldInstance); } return next.filterCandidate(mocks, field, fieldInstance); } }
@Override public String toString() { return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (MockUtil.isMock(mockInstance) ? MockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
public static MockitoException cannotInjectDependency(Field field, Object matchingMock, Exception details) { return new MockitoException(join( "Mockito couldn't inject mock dependency '" + MockUtil.getMockName(matchingMock) + "' on field ", "'" + field + "'", "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.", "Also I failed because: " + exceptionCauseMessageIfAvailable(details), "" ), details); }
public static MockitoException stubPassedToVerify(Object mock) { return new CannotVerifyStubOnlyMock(join( "Argument \"" + MockUtil.getMockName(mock) + "\" passed to verify is a stubOnly() mock which cannot be verified.", "If you intend to verify invocations on this mock, don't use stubOnly() in its MockSettings." )); }
public static MockitoException wrongTypeReturnedByDefaultAnswer(Object mock, String expectedType, String actualType, String methodName) { return new WrongTypeOfReturnValue(join( "Default answer returned a result with the wrong type:", actualType + " cannot be returned by " + methodName + "()", methodName + "() should return " + expectedType, "", "The default answer of " + MockUtil.getMockName(mock) + " that was configured on the mock is probably incorrectly implemented.", "" )); }
public void cannotInjectDependency(Field field, Object matchingMock, Exception details) { throw new MockitoException(join( "Mockito couldn't inject mock dependency '" + new MockUtil().getMockName(matchingMock) + "' on field ", "'" + field + "'", "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.", "Also I failed because: " + details.getCause().getMessage(), "" ), details); }
public String print(List<ArgumentMatcher> matchers, Invocation invocation) { MatchersPrinter matchersPrinter = new MatchersPrinter(); String qualifiedName = MockUtil.getMockName(invocation.getMock()) + "." + invocation.getMethod().getName(); String invocationString = qualifiedName + matchersPrinter.getArgumentsLine(matchers, this); if (isMultiline() || (!matchers.isEmpty() && invocationString.length() > MAX_LINE_LENGTH)) { return qualifiedName + matchersPrinter.getArgumentsBlock(matchers, this); } else { return invocationString; } }
public static MockitoException invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) { return new MockitoException(join( "Invalid argument index for the current invocation of method : ", " -> " + MockUtil.getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", (willReturnLastParameter ? "Last parameter wanted" : "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation), "The index need to be a positive number that indicates a valid position of the argument in the invocation.", "However it is possible to use the -1 value to indicates that the last argument should be returned.", "" )); }
@Override public String toString() { MockUtil mockUtil = new MockUtil(); return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
public String print(List<Matcher> matchers, Invocation invocation) { MatchersPrinter matchersPrinter = new MatchersPrinter(); String qualifiedName = new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName(); String invocationString = qualifiedName + matchersPrinter.getArgumentsLine(matchers, this); if (isMultiline() || (!matchers.isEmpty() && invocationString.length() > MAX_LINE_LENGTH)) { return qualifiedName + matchersPrinter.getArgumentsBlock(matchers, this); } else { return invocationString; } }
public static void maybeRedefineMockName(Object mock, String newName) { MockName mockName = getMockName(mock); //TODO SF hacky... MockCreationSettings mockSettings = getMockHandler(mock).getMockSettings(); if (mockName.isDefault() && mockSettings instanceof CreationSettings) { ((CreationSettings) mockSettings).setMockName(new MockNameImpl(newName)); } }
public static MockitoAssertionError noMoreInteractionsWantedInOrder(Invocation undesired) { return new VerificationInOrderFailure(join( "No interactions wanted here:", new LocationImpl(), "But found this interaction on mock '" + MockUtil.getMockName(undesired.getMock()) + "':", undesired.getLocation() )); }
public Object answer(InvocationOnMock invocation) { if (isToStringMethod(invocation.getMethod())) { Object mock = invocation.getMock(); MockName name = MockUtil.getMockName(mock); if (name.isDefault()) { return "Mock for " + MockUtil.getMockSettings(mock).getTypeToMock().getSimpleName() + ", hashCode: " + mock.hashCode(); } else { return name.toString(); } } else if (isCompareToMethod(invocation.getMethod())) { //see issue 184. //mocks by default should return 0 if references are the same, otherwise some other value because they are not the same. Hence we return 1 (anything but 0 is good). //Only for compareTo() method by the Comparable interface return invocation.getMock() == invocation.getArgument(0) ? 0 : 1; } Class<?> returnType = invocation.getMethod().getReturnType(); return returnValueFor(returnType); }
public int invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) { throw new MockitoException( join("Invalid argument index for the current invocation of method : ", " -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", (willReturnLastParameter ? "Last parameter wanted" : "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation), "The index need to be a positive number that indicates a valid position of the argument in the invocation.", "However it is possible to use the -1 value to indicates that the last argument should be returned.", "")); }
public static MockitoAssertionError noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) { ScenarioPrinter scenarioPrinter = new ScenarioPrinter(); String scenario = scenarioPrinter.print(invocations); return new NoInteractionsWanted(join( "No interactions wanted here:", new LocationImpl(), "But found this interaction on mock '" + MockUtil.getMockName(undesired.getMock()) + "':", undesired.getLocation(), scenario )); }
public void maybeRedefineMockName(Object mock, String newName) { MockName mockName = getMockName(mock); //TODO SF hacky... if (mockName.isDefault() && getMockHandler(mock).getMockSettings() instanceof CreationSettings) { ((CreationSettings) getMockHandler(mock).getMockSettings()).setMockName(new MockNameImpl(newName)); } } }