Search in sources :

Example 81 with Invocation

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

the class InvocationImplTest method shouldBeAbleToCallRealMethod.

@Test
public void shouldBeAbleToCallRealMethod() throws Throwable {
    //when
    Invocation invocation = invocationOf(Foo.class, "bark", new RealMethod() {

        public Object invoke(Object target, Object[] arguments) throws Throwable {
            return new Foo().bark();
        }
    });
    //then
    assertEquals("woof", invocation.callRealMethod());
}
Also used : Invocation(org.mockito.invocation.Invocation) RealMethod(org.mockito.internal.invocation.realmethod.RealMethod) Test(org.junit.Test)

Example 82 with Invocation

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

the class InOrderImplTest method shouldMarkVerifiedInOrder.

@Test
public void shouldMarkVerifiedInOrder() throws Exception {
    //given
    InOrderImpl impl = new InOrderImpl(singletonList(mock));
    Invocation i = new InvocationBuilder().toInvocation();
    assertFalse(impl.isVerified(i));
    //when
    impl.markVerified(i);
    //then
    assertTrue(impl.isVerified(i));
}
Also used : Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 83 with Invocation

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

the class InvocationContainerImplStubbingTest method should_get_results_for_methods_stub_only.

@Test
public void should_get_results_for_methods_stub_only() throws Throwable {
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
    invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod"));
    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()));
    assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod));
    try {
        invocationContainerImplStubOnly.answerTo(differentMethod);
        fail();
    } catch (MyException e) {
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 84 with Invocation

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

the class InvocationContainerImplStubbingTest method should_get_results_for_methods.

@Test
public void should_get_results_for_methods() throws Throwable {
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod));
    invocationContainerImpl.addAnswer(new Returns("simpleMethod"));
    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImpl.addAnswer(new ThrowsException(new MyException()));
    assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod));
    try {
        invocationContainerImpl.answerTo(differentMethod);
        fail();
    } catch (MyException e) {
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) ThrowsException(org.mockito.internal.stubbing.answers.ThrowsException) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 85 with Invocation

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

the class AtLeastXNumberOfInvocationsCheckerTest method shouldMarkActualInvocationsAsVerifiedInOrder.

@Test
public void shouldMarkActualInvocationsAsVerifiedInOrder() {
    InOrderContext context = new InOrderContextImpl();
    //given
    Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
    Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
    //when
    checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 1, context);
    //then
    assertThat(invocation.isVerified()).isTrue();
}
Also used : InOrderContextImpl(org.mockito.internal.verification.InOrderContextImpl) Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) InOrderContext(org.mockito.internal.verification.api.InOrderContext) 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