Search in sources :

Example 16 with Returns

use of org.mockito.internal.stubbing.answers.Returns 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 17 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project mockito by mockito.

the class InvocationContainerImplStubbingTest method should_finish_stubbing_on_adding_return_value.

@Test
public void should_finish_stubbing_on_adding_return_value() throws Exception {
    state.stubbingStarted();
    invocationContainerImpl.addAnswer(new Returns("test"));
    state.validateState();
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Test(org.junit.Test)

Example 18 with Returns

use of org.mockito.internal.stubbing.answers.Returns 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 19 with Returns

use of org.mockito.internal.stubbing.answers.Returns 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"));
                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 20 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project spring-security by spring-projects.

the class DelegatingSecurityContextCallableTests method setUp.

@Before
@SuppressWarnings("serial")
public void setUp() throws Exception {
    originalSecurityContext = SecurityContextHolder.createEmptyContext();
    when(delegate.call()).thenAnswer(new Returns(callableResult) {

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            assertThat(SecurityContextHolder.getContext()).isEqualTo(securityContext);
            return super.answer(invocation);
        }
    });
    executor = Executors.newFixedThreadPool(1);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Aggregations

Returns (org.mockito.internal.stubbing.answers.Returns)27 Test (org.junit.Test)12 InvocationOnMock (org.mockito.invocation.InvocationOnMock)8 Invocation (org.mockito.invocation.Invocation)6 Before (org.junit.Before)5 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)5 Context (android.content.Context)4 CloudSpannerConnection (nl.topicus.jdbc.CloudSpannerConnection)4 CloudSpannerResultSet (nl.topicus.jdbc.resultset.CloudSpannerResultSet)4 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)4 Answer (org.mockito.stubbing.Answer)4 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)4 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)3 SuppressLint (android.annotation.SuppressLint)2 AlertDialog (android.app.AlertDialog)2 ContentValues (android.content.ContentValues)2 PackageInfo (android.content.pm.PackageInfo)2 Cursor (android.database.Cursor)2 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)2 Handler (android.os.Handler)2