Search in sources :

Example 71 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class NumberOfInvocationsChecker method checkNumberOfInvocations.

public static void checkNumberOfInvocations(List<Invocation> invocations, MatchableInvocation wanted, int wantedCount, InOrderContext context) {
    List<Invocation> chunk = findMatchingChunk(invocations, wanted, wantedCount, context);
    int actualCount = chunk.size();
    if (wantedCount > actualCount) {
        Location lastInvocation = getLastLocation(chunk);
        throw tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation);
    }
    if (wantedCount < actualCount) {
        Location firstUndesired = chunk.get(wantedCount).getLocation();
        throw tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired);
    }
    markVerifiedInOrder(chunk, wanted, context);
}
Also used : Discrepancy(org.mockito.internal.reporting.Discrepancy) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationsFinder.findFirstMatchingUnverifiedInvocation(org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation) Location(org.mockito.invocation.Location) InvocationsFinder.getLastLocation(org.mockito.internal.invocation.InvocationsFinder.getLastLocation)

Example 72 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class InvocationMarkerTest method shouldMarkInvocationsAsVerifiedInOrder.

@Test
public void shouldMarkInvocationsAsVerifiedInOrder() {
    //given
    InOrderContextImpl context = new InOrderContextImpl();
    Invocation i = new InvocationBuilder().toInvocation();
    InvocationMatcher im = new InvocationBuilder().toInvocationMatcher();
    assertFalse(context.isVerified(i));
    assertFalse(i.isVerified());
    //when
    InvocationMarker.markVerifiedInOrder(Arrays.asList(i), im, context);
    //then
    assertTrue(context.isVerified(i));
    assertTrue(i.isVerified());
}
Also used : InOrderContextImpl(org.mockito.internal.verification.InOrderContextImpl) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) Test(org.junit.Test)

Example 73 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class InvocationMarkerTest method shouldCaptureArguments.

@Test
public void shouldCaptureArguments() {
    //given
    Invocation i = new InvocationBuilder().toInvocation();
    final AtomicReference<Invocation> box = new AtomicReference<Invocation>();
    MatchableInvocation c = new InvocationMatcher(i) {

        public void captureArgumentsFrom(Invocation i) {
            box.set(i);
        }
    };
    //when
    InvocationMarker.markVerified(Arrays.asList(i), c);
    //then
    assertEquals(i, box.get());
}
Also used : MatchableInvocation(org.mockito.invocation.MatchableInvocation) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 74 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class InvocationMatcherTest method should_capture_arguments_from_invocation.

@Test
public void should_capture_arguments_from_invocation() throws Exception {
    //given
    Invocation invocation = new InvocationBuilder().args("1", 100).toInvocation();
    CapturingMatcher capturingMatcher = new CapturingMatcher();
    InvocationMatcher invocationMatcher = new InvocationMatcher(invocation, (List) asList(new Equals("1"), capturingMatcher));
    //when
    invocationMatcher.captureArgumentsFrom(invocation);
    //then
    assertEquals(1, capturingMatcher.getAllValues().size());
    assertEquals(100, capturingMatcher.getLastValue());
}
Also used : Equals(org.mockito.internal.matchers.Equals) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Invocation(org.mockito.invocation.Invocation) CapturingMatcher(org.mockito.internal.matchers.CapturingMatcher) Test(org.junit.Test)

Example 75 with Invocation

use of org.mockito.invocation.Invocation in project mockito by mockito.

the class InvocationMatcherTest method should_know_if_is_similar_to.

@Test
public void should_know_if_is_similar_to() throws Exception {
    Invocation same = new InvocationBuilder().mock(mock).simpleMethod().toInvocation();
    assertTrue(simpleMethod.hasSimilarMethod(same));
    Invocation different = new InvocationBuilder().mock(mock).differentMethod().toInvocation();
    assertFalse(simpleMethod.hasSimilarMethod(different));
}
Also used : Invocation(org.mockito.invocation.Invocation) Test(org.junit.Test)

Aggregations

Invocation (org.mockito.invocation.Invocation)106 Test (org.junit.Test)71 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)28 MatchableInvocation (org.mockito.invocation.MatchableInvocation)19 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)11 Location (org.mockito.invocation.Location)7 InvocationsFinder.getLastLocation (org.mockito.internal.invocation.InvocationsFinder.getLastLocation)6 Returns (org.mockito.internal.stubbing.answers.Returns)6 VerificationDataInOrderImpl (org.mockito.internal.verification.api.VerificationDataInOrderImpl)6 Method (java.lang.reflect.Method)5 Discrepancy (org.mockito.internal.reporting.Discrepancy)4 MissingInvocationChecker.checkMissingInvocation (org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation)4 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)3 InvocationsFinder.findFirstMatchingUnverifiedInvocation (org.mockito.internal.invocation.InvocationsFinder.findFirstMatchingUnverifiedInvocation)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 IMethods (org.mockitousage.IMethods)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 LinkedList (java.util.LinkedList)2 TestCase.assertEquals (junit.framework.TestCase.assertEquals)2 MockingDetails (org.mockito.MockingDetails)2