Search in sources :

Example 1 with Returns

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

the class MockHandlerFactoryTest method valid_handle_result_is_permitted.

@Test
public //see issue 331
void valid_handle_result_is_permitted() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(123));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then
    assertEquals(123, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 2 with Returns

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

the class MockHandlerFactoryTest method handle_result_must_not_be_null_for_primitives.

@Test
public //see issue 331
void handle_result_must_not_be_null_for_primitives() throws Throwable {
    //given:
    MockCreationSettings<?> settings = (MockCreationSettings<?>) new MockSettingsImpl().defaultAnswer(new Returns(null));
    InternalMockHandler<?> handler = createMockHandler(settings);
    mock.intReturningMethod();
    Invocation invocation = super.getLastInvocation();
    //when:
    Object result = handler.handle(invocation);
    //then null value is not a valid result for a primitive
    assertNotNull(result);
    assertEquals(0, result);
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Invocation(org.mockito.invocation.Invocation) MockCreationSettings(org.mockito.mock.MockCreationSettings) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) Test(org.junit.Test)

Example 3 with Returns

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

the class MockHandlerImplTest method should_report_bogus_default_answer.

@Test(expected = WrongTypeOfReturnValue.class)
public void should_report_bogus_default_answer() throws Throwable {
    MockSettingsImpl mockSettings = mock(MockSettingsImpl.class);
    MockHandlerImpl<?> handler = new MockHandlerImpl(mockSettings);
    given(mockSettings.getDefaultAnswer()).willReturn(new Returns(AWrongType.WRONG_TYPE));
    // otherwise cast is not done
    @SuppressWarnings("unused") String there_should_not_be_a_CCE_here = (String) handler.handle(new InvocationBuilder().method(Object.class.getDeclaredMethod("toString")).toInvocation());
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 4 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).iterator();
        verify(databaseManagerMock).switchToInMemory(eq("scan.iterator"), any(RuntimeException.class));
    }
    {
        databaseManagerMock = getDatabaseManagerMock();
        databaseManagerMock.getScanner(null, null).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);
        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);
        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);
        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 5 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)

Aggregations

Returns (org.mockito.internal.stubbing.answers.Returns)14 Test (org.junit.Test)9 Invocation (org.mockito.invocation.Invocation)6 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)5 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)4 Before (org.junit.Before)3 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 Context (android.content.Context)2 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)2 MockCreationSettings (org.mockito.mock.MockCreationSettings)2 Answer (org.mockito.stubbing.Answer)2 IMethods (org.mockitousage.IMethods)2 PowerMockito.doAnswer (org.powermock.api.mockito.PowerMockito.doAnswer)2 SuppressLint (android.annotation.SuppressLint)1 AlertDialog (android.app.AlertDialog)1 ContentValues (android.content.ContentValues)1 PackageInfo (android.content.pm.PackageInfo)1 Cursor (android.database.Cursor)1 SQLiteQueryBuilder (android.database.sqlite.SQLiteQueryBuilder)1