Search in sources :

Example 11 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project mobile-center-sdk-android by Microsoft.

the class AbstractDefaultChannelTest method setUp.

@Before
public void setUp() throws Exception {
    mockStatic(AppCenterLog.class);
    mockStatic(IdHelper.class, new Returns(UUIDUtils.randomUUID()));
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class));
    whenNew(Handler.class).withParameterTypes(Looper.class).withArguments(Looper.getMainLooper()).thenReturn(mHandler);
    when(mCoreHandler.post(any(Runnable.class))).then(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            ((Runnable) invocation.getArguments()[0]).run();
            return true;
        }
    });
    mockStatic(HandlerUtils.class);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            ((Runnable) invocation.getArguments()[0]).run();
            return null;
        }
    }).when(HandlerUtils.class);
    HandlerUtils.runOnUiThread(any(Runnable.class));
}
Also used : Context(android.content.Context) Returns(org.mockito.internal.stubbing.answers.Returns) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Device(com.microsoft.appcenter.ingestion.models.Device) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Handler(android.os.Handler) Before(org.junit.Before)

Example 12 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project mobile-center-sdk-android by Microsoft.

the class DatabaseManagerTest method switchInMemory.

@Test
public void switchInMemory() throws Exception {
    DatabaseManager databaseManagerMock;
    /* Put. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.put(new ContentValues());
    verify(databaseManagerMock).switchToInMemory(eq("put"), any(RuntimeException.class));
    /* Update. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.update(0, new ContentValues());
    verify(databaseManagerMock).switchToInMemory(eq("update"), any(RuntimeException.class));
    /* Get. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.get(0);
    verify(databaseManagerMock).switchToInMemory(eq("get"), any(RuntimeException.class));
    /* Scanner. */
    {
        databaseManagerMock = getDatabaseManagerMock();
        databaseManagerMock.getScanner(null, null, false).iterator();
        verify(databaseManagerMock).switchToInMemory(eq("scan.iterator"), any(RuntimeException.class));
    }
    {
        databaseManagerMock = getDatabaseManagerMock();
        databaseManagerMock.getScanner(null, null, false).getCount();
        verify(databaseManagerMock).switchToInMemory(eq("scan.count"), any(RuntimeException.class));
    }
    {
        /* Cursor next failing but closing working. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        when(cursor.moveToNext()).thenThrow(new RuntimeException());
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
        /* We switched over in memory so closing will not switch again. Cursor is closed already. */
        doThrow(new RuntimeException()).when(cursor).close();
        scanner.close();
        verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    {
        /* Cursor next failing and closing failing. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        when(cursor.moveToNext()).thenThrow(new RuntimeException());
        doThrow(new RuntimeException()).when(cursor).close();
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        verify(databaseManagerMock).switchToInMemory(eq("scan.hasNext"), any(RuntimeException.class));
        /* We switched over in memory so closing will not switch again. Cursor is closed already in hasNext(). */
        scanner.close();
        verify(databaseManagerMock, never()).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    {
        /* Cursor closing failing. */
        databaseManagerMock = spy(new DatabaseManager(null, "database", "table", 1, null, null));
        when(databaseManagerMock.getDatabase()).thenReturn(mock(SQLiteDatabase.class));
        mockStatic(SQLiteUtils.class);
        Cursor cursor = mock(Cursor.class);
        SQLiteQueryBuilder sqLiteQueryBuilder = mock(SQLiteQueryBuilder.class, new Returns(cursor));
        when(SQLiteUtils.newSQLiteQueryBuilder()).thenReturn(sqLiteQueryBuilder);
        doThrow(new RuntimeException()).when(cursor).close();
        DatabaseManager.Scanner scanner = databaseManagerMock.getScanner(null, null, false);
        assertFalse(scanner.iterator().hasNext());
        scanner.close();
        verify(databaseManagerMock).switchToInMemory(eq("scan.close"), any(RuntimeException.class));
    }
    /* Delete. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(0);
    verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
    /* Delete multiple IDs. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(new ArrayList<Long>());
    verify(databaseManagerMock, never()).switchToInMemory(eq("delete"), any(RuntimeException.class));
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.delete(Arrays.asList(new Long[] { 0L, 1L }));
    verify(databaseManagerMock).switchToInMemory(eq("delete"), any(RuntimeException.class));
    /* Clear. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.clear();
    verify(databaseManagerMock).switchToInMemory(eq("clear"), any(RuntimeException.class));
    /* Close. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.close();
    verify(databaseManagerMock).switchToInMemory(eq("close"), any(RuntimeException.class));
    /* Row count. */
    databaseManagerMock = getDatabaseManagerMock();
    databaseManagerMock.getRowCount();
    verify(databaseManagerMock).switchToInMemory(eq("count"), any(RuntimeException.class));
}
Also used : ContentValues(android.content.ContentValues) Returns(org.mockito.internal.stubbing.answers.Returns) Matchers.anyLong(org.mockito.Matchers.anyLong) Cursor(android.database.Cursor) SQLiteQueryBuilder(android.database.sqlite.SQLiteQueryBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 13 with Returns

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type.

@Test(expected = MockitoException.class)
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_has_wrong_return_type() throws Exception {
    Invocation dumb_invocation = new InvocationBuilder().toInvocation();
    IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));
    throw Reporter.delegatedMethodHasWrongReturnType(dumb_invocation.getMethod(), dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 14 with Returns

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

the class ReporterTest method can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_dont_exists.

@Test(expected = MockitoException.class)
public void can_use_print_mock_name_even_when_mock_bogus_default_answer_and_when_reporting_delegate_method_dont_exists() throws Exception {
    Invocation dumb_invocation = new InvocationBuilder().toInvocation();
    IMethods mock_with_bogus_default_answer = mock(IMethods.class, new Returns(false));
    throw Reporter.delegatedMethodDoesNotExistOnDelegate(dumb_invocation.getMethod(), mock_with_bogus_default_answer, String.class);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 15 with Returns

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

the class InlineByteBuddyMockMakerTest method settingsFor.

private static <T> MockCreationSettings<T> settingsFor(Class<T> type, Class<?>... extraInterfaces) {
    MockSettingsImpl<T> mockSettings = new MockSettingsImpl<T>();
    mockSettings.setTypeToMock(type);
    mockSettings.defaultAnswer(new Returns("bar"));
    if (extraInterfaces.length > 0)
        mockSettings.extraInterfaces(extraInterfaces);
    return mockSettings;
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl)

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