public void unfinishedStubbing(Location location) { throw new UnfinishedStubbingException(join( "Unfinished stubbing detected here:", location, "", "E.g. thenReturn() may be missing.", "Examples of correct stubbing:", " when(mock.isOk()).thenReturn(true);", " when(mock.isOk()).thenThrow(exception);", " doThrow(exception).when(mock).someVoidMethod();", "Hints:", " 1. missing thenReturn()", " 2. you are trying to stub a final method, you naughty developer!", " 3: you are stubbing the behaviour of another mock inside before 'thenReturn' instruction if completed", "" )); }
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); } }
public void unfinishedVerificationException(Location location) { UnfinishedVerificationException exception = new UnfinishedVerificationException(join( "Missing method call for verify(mock) here:", location, "", "Example of correct verification:", " verify(mock).doSomething()", "", "Also, this error might show up because you verify either of: final/private/equals()/hashCode() methods.", "Those methods *cannot* be stubbed/verified.", "" )); throw exception; }
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 tooLittleActualInvocationsInOrder(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, Location lastActualLocation) { String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation); throw new VerificationInOrderFailure(join( "Verification in order failure:" + message )); }
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 Location getLastLocation(List<Invocation> invocations) { if (invocations.isEmpty()) { return null; } else { Invocation last = invocations.get(invocations.size() - 1); return last.getLocation(); } }
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; }
public void validateState() { validateMostStuff(); //validate stubbing: if (stubbingInProgress != null) { Location temp = stubbingInProgress; stubbingInProgress = null; reporter.unfinishedStubbing(temp); } }
private void validateMostStuff() { //State is cool when GlobalConfiguration is already loaded //this cannot really be tested functionally because I cannot dynamically mess up org.mockito.configuration.MockitoConfiguration class GlobalConfiguration.validate(); if (verificationMode != null) { Location location = verificationMode.getLocation(); verificationMode = null; reporter.unfinishedVerificationException(location); } getArgumentMatcherStorage().validateState(); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) { List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context); int actualCount = chunk.size(); if (wantedCount > actualCount) { Location lastInvocation = finder.getLastLocation(chunk); reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation); } else if (wantedCount < actualCount) { Location firstUndesired = chunk.get(wantedCount).getLocation(); reporter.tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired); } invocationMarker.markVerifiedInOrder(chunk, wanted, context); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount) { List<Invocation> chunk = finder.findAllMatchingUnverifiedChunks(invocations, wanted, orderingContext); int actualCount = chunk.size(); if (wantedCount > actualCount) { Location lastLocation = finder.getLastLocation(chunk); reporter.tooLittleActualInvocationsInOrder(new AtLeastDiscrepancy(wantedCount, actualCount), wanted, lastLocation); } invocationMarker.markVerifiedInOrder(chunk, wanted, orderingContext); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) { int actualCount = 0; Location lastLocation = null; while( actualCount < wantedCount ){ Invocation next = finder.findFirstMatchingUnverifiedInvocation( invocations, wanted, context ); if( next == null ){ reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastLocation ); } marker.markVerified( next, wanted ); context.markVerified( next ); lastLocation = next.getLocation(); actualCount++; } }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount) { List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted); int actualCount = actualInvocations.size(); if (wantedCount > actualCount) { Location lastLocation = finder.getLastLocation(actualInvocations); reporter.tooLittleActualInvocations(new AtLeastDiscrepancy(wantedCount, actualCount), wanted, lastLocation); } invocationMarker.markVerified(actualInvocations, wanted); }
@Test public void shouldGetLastStackTrace() throws Exception { Location last = finder.getLastLocation(invocations); assertSame(differentMethodInvocation.getLocation(), last); assertNull(finder.getLastLocation(Collections.<Invocation>emptyList())); }
private ProxyInvocation(Object proxy, Method method, Object[] rawArgs, DelegatingMethod mockitoMethod, int sequenceNumber, Location location) { this.rawArgs = rawArgs; this.proxy = proxy; this.method = method; this.sequenceNumber = sequenceNumber; this.location = location; args = ArgumentsProcessor.expandArgs(mockitoMethod, rawArgs); }
public void tooManyActualInvocations(int wantedCount, int actualCount, DescribedInvocation wanted, Location firstUndesired) { String message = createTooManyInvocationsMessage(wantedCount, actualCount, wanted, firstUndesired); throw new TooManyActualInvocations(message); }
public void tooManyActualInvocationsInOrder(int wantedCount, int actualCount, DescribedInvocation wanted, Location firstUndesired) { String message = createTooManyInvocationsMessage(wantedCount, actualCount, wanted, firstUndesired); throw new VerificationInOrderFailure(join( "Verification in order failure:" + message )); }
public void tooLittleActualInvocations(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, Location lastActualLocation) { String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation); throw new TooLittleActualInvocations(message); }
public Location getLocation() { return location; }
public Location getLocation() { return invocation.getLocation(); }
public Location stubbedAt() { return stubbedAt.getLocation(); }
public ThrowsSmartNullPointer(InvocationOnMock unstubbedInvocation, Location location) { this.unstubbedInvocation = unstubbedInvocation; this.location = location; }
@Override public void argumentsAreDifferent(String wanted, String actual, Location actualLocation) { this.wanted = wanted; this.actual = actual; this.actualLocation = actualLocation; }
@Override public void argumentsAreDifferent(String wanted, String actual, Location actualLocation) { this.wantedString = wanted; this.actual = actual; this.actualLocation = actualLocation; }
@Override public void tooLittleActualInvocations(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, Location lastActualLocation) { this.wantedCount = discrepancy.getWantedCount(); this.actualCount = discrepancy.getActualCount(); this.wanted = wanted; this.location = lastActualLocation; }
@Override public void tooManyActualInvocations(int wantedCount, int actualCount, DescribedInvocation wanted, Location firstUndesired) { this.wantedCount = wantedCount; this.actualCount = actualCount; this.wanted = wanted; this.location = firstUndesired; }
@Override public void neverWantedButInvoked(DescribedInvocation wanted, Location firstUndesired) { this.wanted = wanted; this.location = firstUndesired; }
@Override public Location getLocation() { return location; }
Location getLocation();