Search in sources :

Example 21 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(MobileCenterLog.class);
    mockStatic(IdHelper.class, new Returns(UUIDUtils.randomUUID()));
    mockStatic(DeviceInfoHelper.class);
    when(DeviceInfoHelper.getDeviceInfo(any(Context.class))).thenReturn(mock(Device.class));
    mHandler = mock(Handler.class);
    whenNew(Handler.class).withParameterTypes(Looper.class).withArguments(Looper.getMainLooper()).thenReturn(mHandler);
    /* Mock handler for asynchronous Persistence */
    HandlerThread mockHandlerThread = mock(HandlerThread.class);
    Looper mockLooper = mock(Looper.class);
    whenNew(HandlerThread.class).withArguments(THREAD_NAME).thenReturn(mockHandlerThread);
    when(mockHandlerThread.getLooper()).thenReturn(mockLooper);
    Handler mockPersistenceHandler = mock(Handler.class);
    whenNew(Handler.class).withArguments(mockLooper).thenReturn(mockPersistenceHandler);
    when(mockPersistenceHandler.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) Device(com.microsoft.azure.mobile.ingestion.models.Device) Handler(android.os.Handler) Returns(org.mockito.internal.stubbing.answers.Returns) Looper(android.os.Looper) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) HandlerThread(android.os.HandlerThread) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Example 22 with Returns

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

the class AbstractDistributeTest method setUp.

@Before
@SuppressLint("ShowToast")
@SuppressWarnings("ResourceType")
public void setUp() throws Exception {
    Distribute.unsetInstance();
    mockStatic(MobileCenterLog.class);
    mockStatic(MobileCenter.class);
    when(MobileCenter.isEnabled()).thenReturn(true);
    /* First call to com.microsoft.azure.mobile.MobileCenter.isEnabled shall return true, initial state. */
    mockStatic(PreferencesStorage.class);
    when(PreferencesStorage.getBoolean(DISTRIBUTE_ENABLED_KEY, true)).thenReturn(true);
    /* Then simulate further changes to state. */
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            /* Whenever the new state is persisted, make further calls return the new state. */
            boolean enabled = (Boolean) invocation.getArguments()[1];
            when(PreferencesStorage.getBoolean(DISTRIBUTE_ENABLED_KEY, true)).thenReturn(enabled);
            return null;
        }
    }).when(PreferencesStorage.class);
    PreferencesStorage.putBoolean(eq(DISTRIBUTE_ENABLED_KEY), anyBoolean());
    /* Default download id when not found. */
    when(PreferencesStorage.getLong(PREFERENCE_KEY_DOWNLOAD_ID, INVALID_DOWNLOAD_IDENTIFIER)).thenReturn(INVALID_DOWNLOAD_IDENTIFIER);
    /* Mock package manager. */
    when(mContext.getApplicationContext()).thenReturn(mContext);
    when(mContext.getPackageName()).thenReturn("com.contoso");
    when(mActivity.getPackageName()).thenReturn("com.contoso");
    when(mContext.getApplicationInfo()).thenReturn(mApplicationInfo);
    when(mActivity.getApplicationInfo()).thenReturn(mApplicationInfo);
    when(mContext.getPackageManager()).thenReturn(mPackageManager);
    when(mActivity.getPackageManager()).thenReturn(mPackageManager);
    PackageInfo packageInfo = mock(PackageInfo.class);
    when(mPackageManager.getPackageInfo("com.contoso", 0)).thenReturn(packageInfo);
    Whitebox.setInternalState(packageInfo, "packageName", "com.contoso");
    Whitebox.setInternalState(packageInfo, "versionName", "1.2.3");
    Whitebox.setInternalState(packageInfo, "versionCode", 6);
    /* Mock app name and other string resources. */
    Whitebox.setInternalState(mApplicationInfo, "labelRes", 42);
    when(mContext.getString(42)).thenReturn("unit-test-app");
    when(mContext.getString(R.string.mobile_center_distribute_update_dialog_message_optional)).thenReturn("%s%s%d");
    when(mContext.getString(R.string.mobile_center_distribute_update_dialog_message_mandatory)).thenReturn("%s%s%d");
    when(mContext.getString(R.string.mobile_center_distribute_install_ready_message)).thenReturn("%s%s%d");
    /* Mock network. */
    mockStatic(NetworkStateHelper.class);
    mNetworkStateHelper = mock(NetworkStateHelper.class, new Returns(true));
    when(NetworkStateHelper.getSharedInstance(any(Context.class))).thenReturn(mNetworkStateHelper);
    /* Mock some statics. */
    mockStatic(BrowserUtils.class);
    mockStatic(UUIDUtils.class);
    mockStatic(ReleaseDetails.class);
    mockStatic(TextUtils.class);
    mockStatic(InstallerUtils.class);
    when(TextUtils.isEmpty(any(CharSequence.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            CharSequence str = (CharSequence) invocation.getArguments()[0];
            return str == null || str.length() == 0;
        }
    });
    /* Mock Crypto to not crypt. */
    mockStatic(CryptoUtils.class);
    when(CryptoUtils.getInstance(any(Context.class))).thenReturn(mCryptoUtils);
    when(mCryptoUtils.decrypt(anyString())).thenAnswer(new Answer<CryptoUtils.DecryptedData>() {

        @Override
        public CryptoUtils.DecryptedData answer(InvocationOnMock invocation) throws Throwable {
            return new CryptoUtils.DecryptedData(invocation.getArguments()[0].toString(), null);
        }
    });
    when(mCryptoUtils.encrypt(anyString())).thenAnswer(new Answer<String>() {

        @Override
        public String answer(InvocationOnMock invocation) throws Throwable {
            return invocation.getArguments()[0].toString();
        }
    });
    /* Dialog. */
    whenNew(AlertDialog.Builder.class).withAnyArguments().thenReturn(mDialogBuilder);
    when(mDialogBuilder.create()).thenReturn(mDialog);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            when(mDialog.isShowing()).thenReturn(true);
            return null;
        }
    }).when(mDialog).show();
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            when(mDialog.isShowing()).thenReturn(false);
            return null;
        }
    }).when(mDialog).hide();
    /* Toast. */
    mockStatic(Toast.class);
    when(Toast.makeText(any(Context.class), anyInt(), anyInt())).thenReturn(mToast);
    /* Mock Handler .*/
    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) AlertDialog(android.app.AlertDialog) PackageInfo(android.content.pm.PackageInfo) Matchers.anyString(org.mockito.Matchers.anyString) Answer(org.mockito.stubbing.Answer) PowerMockito.doAnswer(org.powermock.api.mockito.PowerMockito.doAnswer) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) NetworkStateHelper(com.microsoft.azure.mobile.utils.NetworkStateHelper) Matchers.anyBoolean(org.mockito.Matchers.anyBoolean) CryptoUtils(com.microsoft.azure.mobile.utils.crypto.CryptoUtils) Before(org.junit.Before) SuppressLint(android.annotation.SuppressLint)

Example 23 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project spanner-jdbc by olavloite.

the class CloudSpannerResultSetTest method getMockResultSet.

static ResultSet getMockResultSet() {
    ResultSet res = mock(ResultSet.class);
    when(res.getString(STRING_COL_NULL)).thenReturn(null);
    when(res.isNull(STRING_COL_NULL)).thenReturn(true);
    when(res.getString(STRING_COL_NOT_NULL)).thenReturn("FOO");
    when(res.isNull(STRING_COL_NOT_NULL)).thenReturn(false);
    when(res.getString(STRING_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(STRING_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getString(STRING_COLINDEX_NOTNULL - 1)).thenReturn("BAR");
    when(res.isNull(STRING_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(STRING_COL_NULL)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COL_NOT_NULL)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COLINDEX_NULL - 1)).thenReturn(Type.string());
    when(res.getColumnType(STRING_COLINDEX_NOTNULL - 1)).thenReturn(Type.string());
    when(res.getBoolean(BOOLEAN_COL_NULL)).thenReturn(false);
    when(res.isNull(BOOLEAN_COL_NULL)).thenReturn(true);
    when(res.getBoolean(BOOLEAN_COL_NOT_NULL)).thenReturn(true);
    when(res.isNull(BOOLEAN_COL_NOT_NULL)).thenReturn(false);
    when(res.getBoolean(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(false);
    when(res.isNull(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getBoolean(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.isNull(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(BOOLEAN_COL_NULL)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COL_NOT_NULL)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COLINDEX_NULL - 1)).thenReturn(Type.bool());
    when(res.getColumnType(BOOLEAN_COLINDEX_NOTNULL - 1)).thenReturn(Type.bool());
    when(res.getDouble(DOUBLE_COL_NULL)).thenReturn(0d);
    when(res.isNull(DOUBLE_COL_NULL)).thenReturn(true);
    when(res.getDouble(DOUBLE_COL_NOT_NULL)).thenReturn(1.123456789d);
    when(res.isNull(DOUBLE_COL_NOT_NULL)).thenReturn(false);
    when(res.getDouble(DOUBLE_COLINDEX_NULL - 1)).thenReturn(0d);
    when(res.isNull(DOUBLE_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getDouble(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(2.123456789d);
    when(res.isNull(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(DOUBLE_COL_NULL)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COL_NOT_NULL)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COLINDEX_NULL - 1)).thenReturn(Type.float64());
    when(res.getColumnType(DOUBLE_COLINDEX_NOTNULL - 1)).thenReturn(Type.float64());
    when(res.getString(BYTES_COL_NULL)).thenReturn(null);
    when(res.isNull(BYTES_COL_NULL)).thenReturn(true);
    when(res.getBytes(BYTES_COL_NOT_NULL)).thenReturn(ByteArray.copyFrom("FOO"));
    when(res.isNull(BYTES_COL_NOT_NULL)).thenReturn(false);
    when(res.getBytes(BYTES_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(BYTES_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getBytes(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(ByteArray.copyFrom("BAR"));
    when(res.isNull(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(BYTES_COL_NULL)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COL_NOT_NULL)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COLINDEX_NULL - 1)).thenReturn(Type.bytes());
    when(res.getColumnType(BYTES_COLINDEX_NOTNULL - 1)).thenReturn(Type.bytes());
    when(res.getLong(LONG_COL_NULL)).thenReturn(0l);
    when(res.isNull(LONG_COL_NULL)).thenReturn(true);
    when(res.getLong(LONG_COL_NOT_NULL)).thenReturn(1l);
    when(res.isNull(LONG_COL_NOT_NULL)).thenReturn(false);
    when(res.getLong(LONG_COLINDEX_NULL - 1)).thenReturn(0l);
    when(res.isNull(LONG_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getLong(LONG_COLINDEX_NOTNULL - 1)).thenReturn(2l);
    when(res.isNull(LONG_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(LONG_COL_NULL)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COL_NOT_NULL)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COLINDEX_NULL - 1)).thenReturn(Type.int64());
    when(res.getColumnType(LONG_COLINDEX_NOTNULL - 1)).thenReturn(Type.int64());
    when(res.getDate(DATE_COL_NULL)).thenAnswer(new Returns(null));
    when(res.isNull(DATE_COL_NULL)).thenAnswer(new Returns(true));
    when(res.getDate(DATE_COL_NOT_NULL)).thenAnswer(new Returns(Date.fromYearMonthDay(2017, 9, 10)));
    when(res.isNull(DATE_COL_NOT_NULL)).thenAnswer(new Returns(false));
    when(res.getDate(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(null));
    when(res.isNull(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(true));
    when(res.getDate(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Date.fromYearMonthDay(2017, 9, 10)));
    when(res.isNull(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(false));
    when(res.getColumnType(DATE_COL_NULL)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COL_NOT_NULL)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COLINDEX_NULL - 1)).thenAnswer(new Returns(Type.date()));
    when(res.getColumnType(DATE_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Type.date()));
    Calendar cal1 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal1.clear();
    cal1.set(2017, 8, 10, 8, 15, 59);
    Calendar cal2 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal2.clear();
    cal2.set(2017, 8, 11, 8, 15, 59);
    when(res.getTimestamp(TIMESTAMP_COL_NULL)).thenReturn(null);
    when(res.isNull(TIMESTAMP_COL_NULL)).thenReturn(true);
    when(res.getTimestamp(TIMESTAMP_COL_NOT_NULL)).thenReturn(Timestamp.of(cal1.getTime()));
    when(res.isNull(TIMESTAMP_COL_NOT_NULL)).thenReturn(false);
    when(res.getTimestamp(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getTimestamp(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(Timestamp.of(cal2.getTime()));
    when(res.isNull(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(TIMESTAMP_COL_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COL_NOT_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COLINDEX_NULL - 1)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIMESTAMP_COLINDEX_NOTNULL - 1)).thenReturn(Type.timestamp());
    Calendar cal3 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal3.clear();
    cal3.set(1970, 0, 1, 14, 6, 15);
    Calendar cal4 = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
    cal4.clear();
    cal4.set(1970, 0, 1, 14, 6, 15);
    when(res.getTimestamp(TIME_COL_NULL)).thenReturn(null);
    when(res.isNull(TIME_COL_NULL)).thenReturn(true);
    when(res.getTimestamp(TIME_COL_NOT_NULL)).thenReturn(Timestamp.of(cal3.getTime()));
    when(res.isNull(TIME_COL_NOT_NULL)).thenReturn(false);
    when(res.getTimestamp(TIME_COLINDEX_NULL - 1)).thenReturn(null);
    when(res.isNull(TIME_COLINDEX_NULL - 1)).thenReturn(true);
    when(res.getTimestamp(TIME_COLINDEX_NOTNULL - 1)).thenReturn(Timestamp.of(cal4.getTime()));
    when(res.isNull(TIME_COLINDEX_NOTNULL - 1)).thenReturn(false);
    when(res.getColumnType(TIME_COL_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COL_NOT_NULL)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COLINDEX_NULL - 1)).thenReturn(Type.timestamp());
    when(res.getColumnType(TIME_COLINDEX_NOTNULL - 1)).thenReturn(Type.timestamp());
    when(res.getLongList(ARRAY_COL_NULL)).thenAnswer(new Returns(null));
    when(res.isNull(ARRAY_COL_NULL)).thenAnswer(new Returns(true));
    when(res.getLongList(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(Arrays.asList(1L, 2L, 3L)));
    when(res.isNull(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(false));
    when(res.getLongList(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(null));
    when(res.isNull(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(true));
    when(res.getLongList(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Arrays.asList(1L, 2L, 3L)));
    when(res.isNull(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(false));
    when(res.getColumnType(ARRAY_COL_NULL)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COLINDEX_NULL - 1)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnType(ARRAY_COLINDEX_NOTNULL - 1)).thenAnswer(new Returns(Type.array(Type.int64())));
    when(res.getColumnIndex(STRING_COL_NOT_NULL)).thenAnswer(new Returns(1));
    when(res.getColumnIndex(UNKNOWN_COLUMN)).thenThrow(IllegalArgumentException.class);
    when(res.getColumnIndex(DATE_COL_NOT_NULL)).thenAnswer(new Returns(DATE_COLINDEX_NOTNULL - 1));
    when(res.getColumnIndex(ARRAY_COL_NOT_NULL)).thenAnswer(new Returns(ARRAY_COLINDEX_NOTNULL - 1));
    when(res.getColumnIndex(ARRAY_COL_NULL)).thenAnswer(new Returns(ARRAY_COLINDEX_NULL - 1));
    when(res.getType()).thenReturn(Type.struct(StructField.of(STRING_COL_NULL, Type.string()), StructField.of(STRING_COL_NOT_NULL, Type.string()), StructField.of(BOOLEAN_COL_NULL, Type.bool()), StructField.of(BOOLEAN_COL_NOT_NULL, Type.bool()), StructField.of(DOUBLE_COL_NULL, Type.float64()), StructField.of(DOUBLE_COL_NOT_NULL, Type.float64()), StructField.of(BYTES_COL_NULL, Type.bytes()), StructField.of(BYTES_COL_NOT_NULL, Type.bytes()), StructField.of(LONG_COL_NULL, Type.int64()), StructField.of(LONG_COL_NOT_NULL, Type.int64()), StructField.of(DATE_COL_NULL, Type.date()), StructField.of(DATE_COL_NOT_NULL, Type.date()), StructField.of(TIMESTAMP_COL_NULL, Type.timestamp()), StructField.of(TIMESTAMP_COL_NOT_NULL, Type.timestamp()), StructField.of(TIME_COL_NULL, Type.timestamp()), StructField.of(TIME_COL_NOT_NULL, Type.timestamp())));
    // Next behaviour.
    when(res.next()).thenReturn(true, true, true, true, false);
    return res;
}
Also used : Returns(org.mockito.internal.stubbing.answers.Returns) Calendar(java.util.Calendar) ResultSet(com.google.cloud.spanner.ResultSet)

Example 24 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project spanner-jdbc by olavloite.

the class InsertWorkerTest method createMocks.

private void createMocks(CloudSpannerConnection connection, String selectSQL, long count, String updateSQL, boolean throwExceptionOnUpdate) throws SQLException {
    when(connection.createCopyConnection()).thenAnswer(new Answer<CloudSpannerConnection>() {

        @Override
        public CloudSpannerConnection answer(InvocationOnMock invocation) throws Throwable {
            CloudSpannerConnection copy = CloudSpannerTestObjects.createConnection();
            createMocks(copy, selectSQL, count, updateSQL);
            return copy;
        }
    });
    CloudSpannerPreparedStatement countStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet countResultSet = mock(CloudSpannerResultSet.class);
    when(countResultSet.next()).thenReturn(true, false);
    when(countResultSet.getLong(1)).thenReturn(count);
    when(countStatement.executeQuery()).thenReturn(countResultSet);
    when(connection.prepareStatement("SELECT COUNT(*) AS C FROM (" + selectSQL + ") Q")).thenReturn(countStatement);
    CloudSpannerPreparedStatement selectStatement = mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet selectResultSet = mock(CloudSpannerResultSet.class);
    CloudSpannerResultSetMetaData metadata = mock(CloudSpannerResultSetMetaData.class);
    when(metadata.getColumnCount()).thenReturn(3);
    when(selectResultSet.next()).then(new Answer<Boolean>() {

        private long called = 0;

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            called++;
            if (called <= count)
                return true;
            return false;
        }
    });
    when(selectResultSet.getObject(1)).then(new Returns(1L));
    when(selectResultSet.getObject(2)).then(new Returns("TWO"));
    when(selectResultSet.getObject(3)).then(new Returns("TO"));
    when(selectResultSet.getMetaData()).thenReturn(metadata);
    when(selectStatement.executeQuery()).thenReturn(selectResultSet);
    when(connection.prepareStatement(selectSQL)).thenReturn(selectStatement);
    CloudSpannerPreparedStatement updateStatement = mock(CloudSpannerPreparedStatement.class);
    if (throwExceptionOnUpdate)
        when(updateStatement.executeUpdate()).thenThrow(SQLException.class);
    else
        when(updateStatement.executeUpdate()).thenReturn(1);
    when(connection.prepareStatement(updateSQL)).thenReturn(updateStatement);
}
Also used : SQLException(java.sql.SQLException) CloudSpannerConnection(nl.topicus.jdbc.CloudSpannerConnection) Returns(org.mockito.internal.stubbing.answers.Returns) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet) CloudSpannerResultSetMetaData(nl.topicus.jdbc.resultset.CloudSpannerResultSetMetaData)

Example 25 with Returns

use of org.mockito.internal.stubbing.answers.Returns in project spanner-jdbc by olavloite.

the class CloudSpannerTestObjects method mockXAMethods.

private static void mockXAMethods(CloudSpannerConnection connection) throws SQLException {
    String checkTable = null;
    try {
        Field field = CloudSpannerXAConnection.class.getDeclaredField("CHECK_TABLE_EXISTENCE");
        field.setAccessible(true);
        checkTable = (String) field.get(null);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    CloudSpannerPreparedStatement ps = Mockito.mock(CloudSpannerPreparedStatement.class);
    CloudSpannerResultSet rs = Mockito.mock(CloudSpannerResultSet.class);
    Mockito.when(rs.next()).thenReturn(true, false);
    Mockito.when(connection.prepareStatement(checkTable)).thenAnswer(new Returns(ps));
    Mockito.when(ps.executeQuery()).thenAnswer(new Returns(rs));
}
Also used : Field(java.lang.reflect.Field) Returns(org.mockito.internal.stubbing.answers.Returns) CloudSpannerPreparedStatement(nl.topicus.jdbc.statement.CloudSpannerPreparedStatement) SQLException(java.sql.SQLException) CloudSpannerResultSet(nl.topicus.jdbc.resultset.CloudSpannerResultSet)

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