Skip to content

Commit 7f39642

Browse files
committed
add sharedPreferences unit test
1 parent 72a4a19 commit 7f39642

File tree

7 files changed

+131
-0
lines changed

7 files changed

+131
-0
lines changed

app/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ dependencies {
4848
testImplementation 'org.powermock:powermock-module-junit4:1.7.4'
4949

5050
testImplementation "org.robolectric:robolectric:3.8"
51+
//数据库、SharedPreferences调试,直接在浏览器查看设备数据库、SharedPreferences数据
52+
debugImplementation 'com.amitshekhar.android:debug-db:1.0.4'
5153
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.support.test.runner.AndroidJUnit4;
4+
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
10+
/**
11+
* Created by Jdqm on 2018-7-17.
12+
*/
13+
// @RunWith 只在混合使用 JUnit3 和 JUnit4 需要,若只使用JUnit4,可省略
14+
@RunWith(AndroidJUnit4.class)
15+
public class SharedPreferenceDaoTest {
16+
17+
public static final String TEST_KEY = "instrumentedTest";
18+
public static final String TEST_STRING = "玉刚说";
19+
20+
SharedPreferenceDao spDao;
21+
22+
@Before
23+
public void setUp() {
24+
spDao = new SharedPreferenceDao(App.getContext());
25+
}
26+
27+
@Test
28+
public void sharedPreferenceDaoWriteRead() {
29+
spDao.put(TEST_KEY, TEST_STRING);
30+
Assert.assertEquals(TEST_STRING, spDao.get(TEST_KEY));
31+
}
32+
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
package="com.jdqm.androidunittest">
44

55
<application
6+
android:name=".App"
67
android:allowBackup="true"
78
android:icon="@mipmap/ic_launcher"
89
android:label="@string/app_name"
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.app.Application;
4+
import android.content.Context;
5+
6+
/**
7+
* Created by Jdqm on 2018-7-17.
8+
*/
9+
public class App extends Application {
10+
private static Context context;
11+
12+
@Override
13+
public void onCreate() {
14+
super.onCreate();
15+
context = this;
16+
}
17+
18+
public static Context getContext() {
19+
return context;
20+
}
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.content.Context;
4+
import android.content.SharedPreferences;
5+
6+
/**
7+
* Created by Jdqm on 2018-7-17.
8+
*/
9+
public class SharedPreferenceDao {
10+
private SharedPreferences sp;
11+
12+
public SharedPreferenceDao(SharedPreferences sp) {
13+
this.sp = sp;
14+
}
15+
16+
public SharedPreferenceDao(Context context) {
17+
this(context.getSharedPreferences("config", Context.MODE_PRIVATE));
18+
}
19+
20+
public void put(String key, String value) {
21+
SharedPreferences.Editor editor = sp.edit();
22+
editor.putString(key, value);
23+
editor.apply();
24+
}
25+
26+
public String get(String key) {
27+
return sp.getString(key, null);
28+
}
29+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.app.Application;
4+
5+
/**
6+
* Created by yangsheng on 2018-7-17.
7+
*/
8+
public class RoboApp extends Application {
9+
10+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.jdqm.androidunittest;
2+
3+
import android.os.Environment;
4+
5+
import org.junit.Assert;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
import org.junit.runner.RunWith;
9+
import org.robolectric.RobolectricTestRunner;
10+
import org.robolectric.RuntimeEnvironment;
11+
import org.robolectric.annotation.Config;
12+
13+
/**
14+
* Created by yangsheng on 2018-7-17.
15+
*/
16+
@RunWith(RobolectricTestRunner.class)
17+
@Config(application = RoboApp.class)
18+
public class SharedPreferenceDaoTest {
19+
20+
public static final String TEST_KEY = "instrumentedTest";
21+
public static final String TEST_STRING = "玉刚说";
22+
23+
SharedPreferenceDao spDao;
24+
25+
@Before
26+
public void setUp() {
27+
spDao = new SharedPreferenceDao(RuntimeEnvironment.application);
28+
}
29+
30+
@Test
31+
public void sharedPreferenceDaoWriteRead() {
32+
spDao.put(TEST_KEY, TEST_STRING);
33+
Assert.assertEquals(TEST_STRING, spDao.get(TEST_KEY));
34+
}
35+
36+
}

0 commit comments

Comments
 (0)