Search in sources :

Example 11 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class InvocationContainerImplStubbingTest method should_validate_throwable_for_void_method.

@Test
public void should_validate_throwable_for_void_method() throws Throwable {
    invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new Exception()));
    try {
        invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
        fail();
    } catch (MockitoException e) {
    }
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) MockitoException(org.mockito.exceptions.base.MockitoException) Test(org.junit.Test)

Example 12 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class InvocationContainerImplStubbingTest method should_add_throwable_for_void_method.

@Test
public void should_add_throwable_for_void_method() throws Throwable {
    invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new MyException()));
    invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
    try {
        invocationContainerImpl.answerTo(simpleMethod);
        fail();
    } catch (MyException e) {
    }
}
Also used : ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) Test(org.junit.Test)

Example 13 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class AtLeastXNumberOfInvocationsCheckerTest method shouldReportTooLittleInvocations.

@Test
public void shouldReportTooLittleInvocations() {
    //given
    Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
    Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
    exception.expect(TooLittleActualInvocations.class);
    exception.expectMessage("iMethods.simpleMethod()");
    exception.expectMessage("Wanted *at least* 2 times");
    exception.expectMessage("But was 1 time");
    //when
    checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2);
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 14 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project powermock by powermock.

the class PowerMockMatchersBinder method bindMatchers.

public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, final Invocation invocation) {
    List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers();
    validateMatchers(invocation, lastMatchers);
    // In Mockito 2.0 LocalizedMatcher no more extend ArgumentMatcher, so new list should be created.
    final List<ArgumentMatcher> argumentMatchers = extractArgumentMatchers(lastMatchers);
    final InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, argumentMatchers) {

        @Override
        public String toString() {
            return invocation.toString();
        }
    };
    return invocationWithMatchers;
}
Also used : LocalizedMatcher(org.mockito.internal.matchers.LocalizedMatcher) ArgumentMatcher(org.mockito.ArgumentMatcher) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher)

Example 15 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class WarningsFinder method find.

public void find(FindingsListener findingsListener) {
    List<Invocation> unusedStubs = new LinkedList<>(this.baseUnusedStubs);
    List<InvocationMatcher> allInvocations = new LinkedList<>(this.baseAllInvocations);
    Iterator<Invocation> unusedIterator = unusedStubs.iterator();
    while (unusedIterator.hasNext()) {
        Invocation unused = unusedIterator.next();
        Iterator<InvocationMatcher> unstubbedIterator = allInvocations.iterator();
        while (unstubbedIterator.hasNext()) {
            InvocationMatcher unstubbed = unstubbedIterator.next();
            if (unstubbed.hasSimilarMethod(unused)) {
                findingsListener.foundStubCalledWithDifferentArgs(unused, unstubbed);
                unusedIterator.remove();
                unstubbedIterator.remove();
            }
        }
    }
    for (Invocation i : unusedStubs) {
        findingsListener.foundUnusedStub(i);
    }
    for (InvocationMatcher i : allInvocations) {
        findingsListener.foundUnstubbed(i);
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) LinkedList(java.util.LinkedList)

Aggregations

InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)25 Test (org.junit.Test)19 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)15 Invocation (org.mockito.invocation.Invocation)13 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)4 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)3 Returns (org.mockito.internal.stubbing.answers.Returns)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 InOrderContext (org.mockito.internal.verification.api.InOrderContext)3 MockitoException (org.mockito.exceptions.base.MockitoException)2 NoInteractionsWanted (org.mockito.exceptions.verification.NoInteractionsWanted)2 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)2 IMethods (org.mockitousage.IMethods)2 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 InvalidUseOfMatchersException (org.mockito.exceptions.misusing.InvalidUseOfMatchersException)1 TooFewActualInvocations (org.mockito.exceptions.verification.TooFewActualInvocations)1 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)1