private EventCollector(Class<Target> targetClass, EventCombiner<Target, Event> combiner, Collection<Predicate<? super Event>> filters) { this.combiner = Objects.requireNonNull(combiner, "Event combiner cannot be null!"); this.eventFilters = ImmutableList.copyOf(filters); this.eventHandlers = selectEventHandlers(filters); List<DescribedInvocation> invocations = new ArrayList<>(); MockSettings settings = Mockito.withSettings() .invocationListeners(report -> invocations.add(report.getInvocation())); this.target = Mockito.mock(targetClass, settings); combiner.accept(Mockito.doAnswer(this::answer).when(target)); if (invocations.size() != 1) throw new IllegalStateException("Single listener interaction was expected! But actual: " + invocations); this.invocation = invocations.get(0); }
public void wantedButNotInvoked(DescribedInvocation wanted, List<? extends DescribedInvocation> invocations) { String allInvocations; if (invocations.isEmpty()) { allInvocations = "Actually, there were zero interactions with this mock.\n"; } else { StringBuilder sb = new StringBuilder("\nHowever, there were other interactions with this mock:\n"); for (DescribedInvocation i : invocations) { sb.append(i.toString()) .append("\n") .append(i.getLocation()) .append("\n\n"); } allInvocations = sb.toString(); } String message = createWantedButNotInvokedMessage(wanted); throw new WantedButNotInvoked(message + allInvocations); }
@Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { if(! active){ return; } DescribedInvocation di = methodInvocationReport.getInvocation(); MethodDescriptor md = null; if(di instanceof InvocationOnMock){ InvocationOnMock impl = (InvocationOnMock) di; Method method = impl.getMethod(); md = new MethodDescriptor(method, retvalType); } else { //hopefully it should never happen md = getMethodDescriptor_old(di); } if(md.getMethodName().equals("finalize")){ //ignore it, otherwise if we mock it, we ll end up in a lot of side effects... :( return; } if(onlyMockAbstractMethods() && !md.getGenericMethod().isAbstract()) { return; } synchronized (map){ MethodDescriptor current = map.get(md.getID()); if(current == null){ current = md; } current.increaseCounter(); map.put(md.getID(),current); } }
@Deprecated private MethodDescriptor getMethodDescriptor_old(DescribedInvocation di) { /* Current Mockito API seems quite limited. Here, to know what was called, it looks like the only way is to parse the results of toString. We can identify primitive types and String, but likely not the exact type of input objects. This is a problem if methods are overloaded and having same number of input parameters :( */ String description = di.toString(); int openingP = description.indexOf('('); assert openingP >= 0; String[] leftTokens = description.substring(0,openingP).split("\\."); String className = ""; //TODO String methodName = leftTokens[leftTokens.length-1]; int closingP = description.lastIndexOf(')'); String[] inputTokens = description.substring(openingP+1, closingP).split(","); String mockitoMatchers = ""; if(inputTokens.length > 0) { /* TODO: For now it does not seem really feasible to infer the correct types. Left a feature request on Mockito mailing list, let's see if it ll be done */ mockitoMatchers += "any()"; for (int i=1; i<inputTokens.length; i++) { mockitoMatchers += " , any()"; } } return new MethodDescriptor(className,methodName,mockitoMatchers); }
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 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 static MockSettings async(MockSettings settings) { return settings.invocationListeners(new InvocationListener() { @Override public void reportInvocation(MethodInvocationReport methodInvocationReport) { DescribedInvocation invocation = methodInvocationReport.getInvocation(); if (invocation instanceof InvocationOnMock) { Object mock = ((InvocationOnMock) invocation).getMock(); synchronized (mock) { mock.notifyAll(); } } } }); }
public void wantedButNotInvoked(DescribedInvocation wanted) { throw new WantedButNotInvoked(createWantedButNotInvokedMessage(wanted)); }
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 StubInfoImpl(DescribedInvocation stubbedAt) { this.stubbedAt = stubbedAt; }
public void markStubUsed(DescribedInvocation usedAt) { this.usedAt = usedAt; }
public DescribedInvocation getInvocation() { return invocation; }
private void printInvocation(DescribedInvocation invocation) { printStream.println(invocation.toString()); // printStream.println("Handling method call on a mock/spy."); printlnIndented("invoked: " + invocation.getLocation().toString()); }
@Override public void wantedButNotInvoked(DescribedInvocation wanted, List<? extends DescribedInvocation> invocations) { this.wanted = wanted; }
@Override public void wantedButNotInvokedInOrder(DescribedInvocation wanted, DescribedInvocation previous) { this.wanted = wanted; this.previous = previous; }
@Override public void wantedButNotInvoked(DescribedInvocation wanted) { this.wanted = wanted; }
@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; }
/** * The return type is deprecated, please assign the return value from this method * to the {@link DescribedInvocation} type. Sorry for inconvenience but we had to move * {@link PrintableInvocation} to better place to keep the API consistency. * * @return Information on the method call, never {@code null} */ DescribedInvocation getInvocation();