Search in sources :

Example 21 with InvocationMatcher

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

the class MockHandlerImplTest method should_remove_verification_mode_even_when_invalid_matchers.

@Test
public void should_remove_verification_mode_even_when_invalid_matchers() throws Throwable {
    // given
    Invocation invocation = new InvocationBuilder().toInvocation();
    @SuppressWarnings("rawtypes") MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl());
    mockingProgress().verificationStarted(VerificationModeFactory.atLeastOnce());
    handler.matchersBinder = new MatchersBinder() {

        public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) {
            throw new InvalidUseOfMatchersException();
        }
    };
    try {
        // when
        handler.handle(invocation);
        // then
        fail();
    } catch (InvalidUseOfMatchersException ignored) {
    }
    assertNull(mockingProgress().pullVerificationMode());
}
Also used : InvalidUseOfMatchersException(org.mockito.exceptions.misusing.InvalidUseOfMatchersException) Invocation(org.mockito.invocation.Invocation) MatchersBinder(org.mockito.internal.invocation.MatchersBinder) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) ArgumentMatcherStorage(org.mockito.internal.progress.ArgumentMatcherStorage) Test(org.junit.Test)

Example 22 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher 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"), null);
    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException()), null);
    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 23 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher 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"), null);
    Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation();
    invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod));
    invocationContainerImpl.addAnswer(new ThrowsException(new MyException()), null);
    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 24 with InvocationMatcher

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

the class InvocationContainerImplTest method doShouldBeThreadSafe.

// works 50% of the time
private void doShouldBeThreadSafe(final InvocationContainerImpl c) throws Throwable {
    // given
    Thread[] t = new Thread[200];
    final CountDownLatch starter = new CountDownLatch(200);
    for (int i = 0; i < t.length; i++) {
        t[i] = new Thread() {

            public void run() {
                try {
                    // NOPMD
                    starter.await();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                c.setInvocationForPotentialStubbing(new InvocationMatcher(invocation));
                c.addAnswer(new Returns("foo"), null);
                c.findAnswerFor(invocation);
            }
        };
        t[i].setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {

            public void uncaughtException(Thread t, Throwable e) {
                exceptions.add(e);
            }
        });
        t[i].start();
        starter.countDown();
    }
    // when
    for (Thread aT : t) {
        aT.join();
    }
    // then
    if (exceptions.size() != 0) {
        throw exceptions.getFirst();
    }
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 25 with InvocationMatcher

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

the class InvocationContainerImplTest method should_return_invoked_mock.

@Test
public void should_return_invoked_mock() throws Exception {
    container.setInvocationForPotentialStubbing(new InvocationMatcher(invocation));
    assertEquals(invocation.getMock(), container.invokedMock());
}
Also used : InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) Test(org.junit.Test)

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