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