public void incorrectUseOfAdditionalMatchers(String additionalMatcherName, int expectedSubMatchersCount, Collection<LocalizedMatcher> matcherStack) { throw new InvalidUseOfMatchersException(join( "Invalid use of argument matchers inside additional matcher " + additionalMatcherName + " !", new LocationImpl(), "", expectedSubMatchersCount + " sub matchers expected, " + matcherStack.size() + " recorded:", locationsOf(matcherStack), "", "This exception may occur if matchers are combined with raw values:", " //incorrect:", " someMethod(AdditionalMatchers.and(isNotNull(), \"raw String\");", "When using matchers, all arguments have to be provided by matchers.", "For example:", " //correct:", " someMethod(AdditionalMatchers.and(isNotNull(), eq(\"raw String\"));", "", "For more info see javadoc for Matchers and AdditionalMatchers classes.", "" )); }
public void argumentsAreDifferent(String wanted, String actual, Location actualLocation) { String message = join("Argument(s) are different! Wanted:", wanted, new LocationImpl(), "Actual invocation has different arguments:", actual, actualLocation, "" ); if (JUnitTool.hasJUnit()) { throw JUnitTool.createArgumentsAreDifferentException(message, wanted, actual); } else { throw new ArgumentsAreDifferent(message); } }
@Test public void shouldBeSafeInCaseForSomeReasonFilteredStackTraceIsEmpty() { //given StackTraceFilter filterReturningEmptyArray = new StackTraceFilter() { @Override public StackTraceElement[] filter(StackTraceElement[] target, boolean keepTop) { return new StackTraceElement[0]; } }; //when String loc = new LocationImpl(filterReturningEmptyArray).toString(); //then assertEquals("-> at <<unknown line>>", loc); }
/** * Intercept a method call. Called <u>before</u> a method is called by the proxied method. * * <p>This does the same as {@link #interceptEntryHook(Object, Method, Object[], * InterceptedInvocation.SuperMethod)} but this handles proxied methods. We only proxy abstract * methods. * * @param proxy proxies object * @param method method that was called * @param argsIn arguments to the method * * @return mocked result * @throws Throwable An exception if thrown */ @Override public Object invoke(final Object proxy, final Method method, Object[] argsIn) throws Throwable { // args can be null if the method invoked has no arguments, but Mockito expects a non-null // array final Object[] args = argsIn != null ? argsIn : new Object[0]; if (isEqualsMethod(method)) { return proxy == args[0]; } else if (isHashCodeMethod(method)) { return System.identityHashCode(proxy); } return handler.handle(new ProxyInvocation(proxy, method, args, new DelegatingMethod (method), SequenceNumber.next(), new LocationImpl())); }
public void incorrectUseOfApi() { throw new MockitoException(join( "Incorrect use of API detected here:", new LocationImpl(), "", "You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.", "Examples of correct usage:", " when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);", " when(mock.isOk()).thenReturn(true, false).thenThrow(exception);", "" )); }
public void reportNoSubMatchersFound(String additionalMatcherName) { throw new InvalidUseOfMatchersException(join( "No matchers found for additional matcher " + additionalMatcherName, new LocationImpl(), "" )); }
private String createWantedButNotInvokedMessage(DescribedInvocation wanted) { return join( "Wanted but not invoked:", wanted.toString(), new LocationImpl(), "" ); }
public void wantedButNotInvokedInOrder(DescribedInvocation wanted, DescribedInvocation previous) { throw new VerificationInOrderFailure(join( "Verification in order failure", "Wanted but not invoked:", wanted.toString(), new LocationImpl(), "Wanted anywhere AFTER following interaction:", previous.toString(), previous.getLocation(), "" )); }
private String createTooManyInvocationsMessage(int wantedCount, int actualCount, DescribedInvocation wanted, Location firstUndesired) { return join( wanted.toString(), "Wanted " + pluralize(wantedCount) + ":", new LocationImpl(), "But was " + pluralize(actualCount) + ". Undesired invocation:", firstUndesired, "" ); }
public void neverWantedButInvoked(DescribedInvocation wanted, Location firstUndesired) { throw new NeverWantedButInvoked(join( wanted.toString(), "Never wanted here:", new LocationImpl(), "But invoked here:", firstUndesired, "" )); }
private String createTooLittleInvocationsMessage(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, Location lastActualInvocation) { String ending = (lastActualInvocation != null)? lastActualInvocation + "\n" : "\n"; String message = join( wanted.toString(), "Wanted " + discrepancy.getPluralizedWantedCount() + ":", new LocationImpl(), "But was " + discrepancy.getPluralizedActualCount() + ":", ending ); return message; }
public void noMoreInteractionsWanted(Invocation undesired, List<VerificationAwareInvocation> invocations) { ScenarioPrinter scenarioPrinter = new ScenarioPrinter(); String scenario = scenarioPrinter.print(invocations); throw new NoInteractionsWanted(join( "No interactions wanted here:", new LocationImpl(), "But found this interaction:", undesired.getLocation(), scenario )); }
public void noMoreInteractionsWantedInOrder(Invocation undesired) { throw new VerificationInOrderFailure(join( "No interactions wanted here:", new LocationImpl(), "But found this interaction:", undesired.getLocation(), "" )); }
public void smartNullPointerException(String invocation, Location location) { throw new SmartNullPointerException(join( "You have a NullPointerException here:", new LocationImpl(), "because this method call was *not* stubbed correctly:", location, invocation, "" )); }
public InvocationImpl(Object mock, MockitoMethod mockitoMethod, Object[] args, int sequenceNumber, RealMethod realMethod) { this.method = mockitoMethod; this.mock = mock; this.realMethod = realMethod; this.arguments = ArgumentsProcessor.expandVarArgs(mockitoMethod.isVarArgs(), args); this.rawArguments = args; this.sequenceNumber = sequenceNumber; this.location = new LocationImpl(); }
public Object answer(final InvocationOnMock invocation) throws Throwable { Object defaultReturnValue = delegate.answer(invocation); if (defaultReturnValue != null) { return defaultReturnValue; } Class<?> type = invocation.getMethod().getReturnType(); if (!type.isPrimitive() && !Modifier.isFinal(type.getModifiers())) { final Location location = new LocationImpl(); return Mockito.mock(type, new ThrowsSmartNullPointer(invocation, location)); } return null; }
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // args can be null if the method invoked has no arguments, but Mockito expects a non-null array args = args != null ? args : new Object[0]; if (isEqualsMethod(method)) { return proxy == args[0]; } else if (isHashCodeMethod(method)) { return System.identityHashCode(proxy); } return handler.handle(new ProxyInvocation(proxy, method, args, new DelegatingMethod (method), SequenceNumber.next(), new LocationImpl())); }
/** * Create a new invocation. * * @param mock mocked instance * @param method method invoked * @param arguments arguments to the method * @param superMethod super method * @param sequenceNumber sequence number of the invocation */ InterceptedInvocation(Object mock, MockitoMethod method, Object[] arguments, SuperMethod superMethod, int sequenceNumber) { this.mock = mock; this.method = method; this.arguments = ArgumentsProcessor.expandArgs(method, arguments); this.rawArguments = arguments; this.superMethod = superMethod; this.sequenceNumber = sequenceNumber; location = new LocationImpl(); }
public LocalizedMatcher(Matcher actualMatcher) { this.actualMatcher = actualMatcher; this.location = new LocationImpl(); }
public void stubbingStarted() { validateState(); stubbingInProgress = new LocationImpl(); }
@Test public void shouldLocationNotContainGetStackTraceMethod() { assertContains("shouldLocationNotContainGetStackTraceMethod", new LocationImpl().toString()); }