### Question:
IABSharedPreference { @Nullable @VisibleForTesting String readString(@NonNull SharedPreferences sharedPreferences, @NonNull String key, @Nullable String defValue) { try { String result = sharedPreferences.getString(key, defValue); return result != null ? result : defValue; } catch (Exception e) { return defValue; } } IABSharedPreference();   }### Answer:
@Test public void readString_valueNotExist() { SharedPreferences sharedPreferences = mock(SharedPreferences.class); String defValue = "def_value"; String result = iabSharedPreference.readString(sharedPreferences, "test_key", defValue); assertNotNull(result); assertEquals(defValue, result); }
@Test public void readString_exception() { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); sharedPreferences.edit().putInt("test_key", 123).apply(); String defValue = "def_value"; String result = iabSharedPreference.readString(sharedPreferences, "test_key", defValue); assertNotNull(result); assertEquals(defValue, result); }