File tree Expand file tree Collapse file tree 7 files changed +131
-0
lines changed
androidTest/java/com/jdqm/androidunittest
java/com/jdqm/androidunittest
test/java/com/jdqm/androidunittest Expand file tree Collapse file tree 7 files changed +131
-0
lines changed Original file line number Diff line number Diff line change @@ -48,4 +48,6 @@ dependencies {
48
48
testImplementation ' org.powermock:powermock-module-junit4:1.7.4'
49
49
50
50
testImplementation " org.robolectric:robolectric:3.8"
51
+ // 数据库、SharedPreferences调试,直接在浏览器查看设备数据库、SharedPreferences数据
52
+ debugImplementation ' com.amitshekhar.android:debug-db:1.0.4'
51
53
}
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 3
3
package =" com.jdqm.androidunittest" >
4
4
5
5
<application
6
+ android : name =" .App"
6
7
android : allowBackup =" true"
7
8
android : icon =" @mipmap/ic_launcher"
8
9
android : label =" @string/app_name"
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments