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);
}
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);
}
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());
}
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));
}
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);
}
Aggregations