Search in sources :

Example 21 with Invocation

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

the class ScenarioPrinterTest method shouldNotPrintInvocationsWhenSingleUnwanted.

@Test
public void shouldNotPrintInvocationsWhenSingleUnwanted() {
    //given
    Invocation unverified = new InvocationBuilder().differentMethod().toInvocation();
    //when
    String out = sp.print((List) asList(unverified));
    //then
    assertThat(out).contains("Actually, above is the only interaction with this mock.");
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 22 with Invocation

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

the class CallsRealMethodsTest method should_fail_when_calling_real_method_on_interface.

@Test
public void should_fail_when_calling_real_method_on_interface() throws Throwable {
    //given
    Invocation invocationOnInterface = new InvocationBuilder().method("simpleMethod").toInvocation();
    try {
        //when
        new CallsRealMethods().validateFor(invocationOnInterface);
        //then
        Assertions.fail("can not invoke interface");
    } catch (MockitoException expected) {
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) MockitoException(org.mockito.exceptions.base.MockitoException) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 23 with Invocation

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

the class CallsRealMethodsTest method should_invoke_real_method.

@Test
public void should_invoke_real_method() throws Throwable {
    class Concrete {

        void concrete() {
            throw new RuntimeException("real code");
        }
    }
    Concrete mock = mock(Concrete.class);
    Invocation concrete = new InvocationBuilder().mock(mock).method(Concrete.class.getDeclaredMethod("concrete")).toInvocation();
    try {
        new CallsRealMethods().answer(concrete);
    } catch (RuntimeException throwable) {
        throwable.printStackTrace();
        assertThat(throwable).hasMessage("real code");
    }
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 24 with Invocation

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

the class DefaultRegisteredInvocationsTest method should_not_return_to_string_method.

@Test
public void should_not_return_to_string_method() throws Exception {
    Invocation toString = new InvocationBuilder().method("toString").toInvocation();
    Invocation simpleMethod = new InvocationBuilder().simpleMethod().toInvocation();
    invocations.add(toString);
    invocations.add(simpleMethod);
    assertTrue(invocations.getAll().contains(simpleMethod));
    assertFalse(invocations.getAll().contains(toString));
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 25 with Invocation

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

the class NoMoreInteractionsTest method shouldVerifyInOrderMultipleInvoctions.

@Test
public void shouldVerifyInOrderMultipleInvoctions() {
    //given
    NoMoreInteractions n = new NoMoreInteractions();
    Invocation i = new InvocationBuilder().seq(1).toInvocation();
    Invocation i2 = new InvocationBuilder().seq(2).toInvocation();
    //when
    context.markVerified(i2);
    //then no exception is thrown
    n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null));
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) VerificationDataInOrderImpl(org.mockito.internal.verification.api.VerificationDataInOrderImpl) 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