|
|
@@ -1,68 +1,77 @@
|
|
|
package kr.co.zumo.app.lifeplus.model;
|
|
|
|
|
|
import android.content.SharedPreferences;
|
|
|
-import android.preference.PreferenceManager;
|
|
|
|
|
|
import org.junit.Before;
|
|
|
import org.junit.Test;
|
|
|
-import org.junit.runner.RunWith;
|
|
|
-import org.robolectric.Robolectric;
|
|
|
-import org.robolectric.RobolectricTestRunner;
|
|
|
+import org.mockito.internal.verification.Times;
|
|
|
|
|
|
import java.util.HashSet;
|
|
|
import java.util.Set;
|
|
|
|
|
|
-import kr.co.zumo.app.lifeplus.activity.ActivityBase;
|
|
|
-import kr.co.zumo.app.lifeplus.activity.MainActivity;
|
|
|
+import static org.mockito.ArgumentMatchers.any;
|
|
|
+import static org.mockito.ArgumentMatchers.eq;
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
+import static org.mockito.Mockito.verify;
|
|
|
+import static org.powermock.api.mockito.PowerMockito.when;
|
|
|
|
|
|
-import static junit.framework.Assert.assertEquals;
|
|
|
-
|
|
|
-@RunWith(RobolectricTestRunner.class)
|
|
|
public class BaseSharedPreferencesTest {
|
|
|
|
|
|
SharedPreferences preferences;
|
|
|
BaseSharedPreferences baseSharedPreferences;
|
|
|
+ SharedPreferences.Editor editor;
|
|
|
|
|
|
@Before
|
|
|
public void setUp() throws Exception {
|
|
|
- ActivityBase activity = Robolectric.setupActivity(MainActivity.class);
|
|
|
- preferences = PreferenceManager.getDefaultSharedPreferences(activity.getApplicationContext());
|
|
|
+ preferences = mock(SharedPreferences.class);
|
|
|
+ editor = mock(SharedPreferences.Editor.class);
|
|
|
+ when(preferences.edit()).thenReturn(editor);
|
|
|
baseSharedPreferences = new BaseSharedPreferences(preferences);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void put() {
|
|
|
+ when(editor.putString(any(), any())).thenReturn(editor);
|
|
|
baseSharedPreferences.put("key", "value");
|
|
|
|
|
|
- assertEquals("value", baseSharedPreferences.get("key", ""));
|
|
|
+ verify(editor).putString(eq("key"), eq("value"));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void put1() {
|
|
|
+ when(editor.putBoolean(any(), eq(true))).thenReturn(editor);
|
|
|
baseSharedPreferences.put("key", true);
|
|
|
|
|
|
- assertEquals(true, baseSharedPreferences.get("key", false));
|
|
|
+ verify(editor).putBoolean(eq("key"), eq(true));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void put2() {
|
|
|
+ when(editor.putInt(any(), eq(Integer.MAX_VALUE))).thenReturn(editor);
|
|
|
baseSharedPreferences.put("key", Integer.MAX_VALUE);
|
|
|
|
|
|
- assertEquals(Integer.MAX_VALUE, baseSharedPreferences.get("key", 0));
|
|
|
+ verify(editor).putInt(eq("key"), eq(Integer.MAX_VALUE));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void put3() {
|
|
|
+ when(editor.putLong(any(), eq(Long.MAX_VALUE))).thenReturn(editor);
|
|
|
baseSharedPreferences.put("key", Long.MAX_VALUE);
|
|
|
|
|
|
- assertEquals(Long.MAX_VALUE, baseSharedPreferences.get("key", 0L));
|
|
|
+ verify(editor).putLong(eq("key"), eq(Long.MAX_VALUE));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void put4() {
|
|
|
+ when(editor.putFloat(any(), eq(Float.MAX_VALUE))).thenReturn(editor);
|
|
|
baseSharedPreferences.put("key", Float.MAX_VALUE);
|
|
|
|
|
|
- assertEquals(Float.MAX_VALUE, baseSharedPreferences.get("key", 0F));
|
|
|
+ verify(editor).putFloat(eq("key"), eq(Float.MAX_VALUE));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
@@ -71,20 +80,27 @@ public class BaseSharedPreferencesTest {
|
|
|
stringSet.add("A");
|
|
|
stringSet.add("B");
|
|
|
stringSet.add("C");
|
|
|
+ when(editor.putStringSet(any(), eq(stringSet))).thenReturn(editor);
|
|
|
+
|
|
|
baseSharedPreferences.put("key", stringSet);
|
|
|
|
|
|
- Set<String> stringSetDefault = new HashSet<>();
|
|
|
- assertEquals(stringSet.toString(), baseSharedPreferences.get("key", stringSetDefault).toString());
|
|
|
+ verify(editor).putStringSet(eq("key"), eq(stringSet));
|
|
|
+ verify(editor).apply();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
public void remove() {
|
|
|
- baseSharedPreferences.put("key", 111);
|
|
|
+ when(editor.putString(any(), any())).thenReturn(editor);
|
|
|
+ when(editor.remove(any())).thenReturn(editor);
|
|
|
+
|
|
|
+ baseSharedPreferences.put("key", "value");
|
|
|
|
|
|
- assertEquals(111, baseSharedPreferences.get("key", 0));
|
|
|
+ verify(editor).putString(eq("key"), eq("value"));
|
|
|
+ verify(editor).apply();
|
|
|
|
|
|
baseSharedPreferences.remove("key");
|
|
|
|
|
|
- assertEquals(0, baseSharedPreferences.get("key", 0));
|
|
|
+ verify(editor).remove(eq("key"));
|
|
|
+ verify(editor, new Times(2)).apply();
|
|
|
}
|
|
|
}
|