@@ -22,12 +22,10 @@ import androidx.test.core.app.ApplicationProvider.getApplicationContext
22
22
import androidx.test.ext.junit.runners.AndroidJUnit4
23
23
import androidx.test.filters.SmallTest
24
24
import com.example.android.architecture.blueprints.todoapp.MainCoroutineRule
25
+ import junit.framework.TestCase.assertEquals
26
+ import junit.framework.TestCase.assertNotNull
25
27
import kotlinx.coroutines.ExperimentalCoroutinesApi
26
28
import kotlinx.coroutines.test.runTest
27
- import org.hamcrest.CoreMatchers.`is`
28
- import org.hamcrest.CoreMatchers.notNullValue
29
- import org.hamcrest.MatcherAssert.assertThat
30
- import org.junit.After
31
29
import org.junit.Before
32
30
import org.junit.Rule
33
31
import org.junit.Test
@@ -38,7 +36,12 @@ import org.junit.runner.RunWith
38
36
@SmallTest
39
37
class TasksDaoTest {
40
38
41
- private lateinit var database: ToDoDatabase
39
+ // using an in-memory database because the information stored here disappears when the
40
+ // process is killed
41
+ private val database = Room .inMemoryDatabaseBuilder(
42
+ getApplicationContext(),
43
+ ToDoDatabase ::class .java
44
+ ).allowMainThreadQueries().build()
42
45
43
46
// Set the main coroutines dispatcher for unit testing.
44
47
@ExperimentalCoroutinesApi
@@ -49,18 +52,9 @@ class TasksDaoTest {
49
52
@get:Rule
50
53
val instantExecutorRule = InstantTaskExecutorRule ()
51
54
55
+ // Ensure that we use an empty database for each test.
52
56
@Before
53
- fun initDb () {
54
- // using an in-memory database because the information stored here disappears when the
55
- // process is killed
56
- database = Room .inMemoryDatabaseBuilder(
57
- getApplicationContext(),
58
- ToDoDatabase ::class .java
59
- ).allowMainThreadQueries().build()
60
- }
61
-
62
- @After
63
- fun closeDb () = database.close()
57
+ fun initDb () = database.clearAllTables()
64
58
65
59
@Test
66
60
fun insertTaskAndGetById () = runTest {
@@ -72,11 +66,11 @@ class TasksDaoTest {
72
66
val loaded = database.taskDao().getTaskById(task.id)
73
67
74
68
// THEN - The loaded data contains the expected values
75
- assertThat< LocalTask > (loaded as LocalTask , notNullValue() )
76
- assertThat(loaded .id, ` is `(task .id) )
77
- assertThat(loaded .title, ` is `(task .title) )
78
- assertThat(loaded .description, ` is `(task .description) )
79
- assertThat(loaded .isCompleted, ` is `(task .isCompleted) )
69
+ assertNotNull (loaded as LocalTask )
70
+ assertEquals(task .id, loaded .id)
71
+ assertEquals(task .title, loaded .title)
72
+ assertEquals(task .description, loaded .description)
73
+ assertEquals(task .isCompleted, loaded .isCompleted)
80
74
}
81
75
82
76
@Test
@@ -96,10 +90,10 @@ class TasksDaoTest {
96
90
97
91
// THEN - The loaded data contains the expected values
98
92
val loaded = database.taskDao().getTaskById(task.id)
99
- assertThat(loaded? .id, ` is `(task .id) )
100
- assertThat( loaded?.title, ` is `( " title2 " ) )
101
- assertThat( loaded?.description, ` is `( " description2 " ) )
102
- assertThat( loaded?.isCompleted, ` is `( true ) )
93
+ assertEquals(task .id, loaded? .id)
94
+ assertEquals( " title2 " , loaded?.title)
95
+ assertEquals( " description2 " , loaded?.description)
96
+ assertEquals( true , loaded?.isCompleted)
103
97
}
104
98
105
99
@Test
@@ -112,11 +106,11 @@ class TasksDaoTest {
112
106
val tasks = database.taskDao().getTasks()
113
107
114
108
// THEN - There is only 1 task in the database, and contains the expected values
115
- assertThat( tasks.size, ` is `( 1 ) )
116
- assertThat (tasks[0 ].id, ` is `( task.id) )
117
- assertThat (tasks[0 ].title, ` is `( task.title) )
118
- assertThat (tasks[0 ].description, ` is `( task.description) )
119
- assertThat (tasks[0 ].isCompleted, ` is `( task.isCompleted) )
109
+ assertEquals( 1 , tasks.size)
110
+ assertEquals (tasks[0 ].id, task.id)
111
+ assertEquals (tasks[0 ].title, task.title)
112
+ assertEquals (tasks[0 ].description, task.description)
113
+ assertEquals (tasks[0 ].isCompleted, task.isCompleted)
120
114
}
121
115
122
116
@Test
@@ -136,10 +130,10 @@ class TasksDaoTest {
136
130
137
131
// THEN - The loaded data contains the expected values
138
132
val loaded = database.taskDao().getTaskById(originalTask.id)
139
- assertThat(loaded? .id, ` is `(originalTask .id) )
140
- assertThat(loaded?.title, ` is `( " new title" ) )
141
- assertThat(loaded?.description, ` is `( " new description" ) )
142
- assertThat( loaded?.isCompleted, ` is `( true ) )
133
+ assertEquals(originalTask .id, loaded? .id)
134
+ assertEquals( " new title" , loaded?.title )
135
+ assertEquals( " new description" , loaded?.description )
136
+ assertEquals( true , loaded?.isCompleted)
143
137
}
144
138
145
139
@Test
@@ -158,10 +152,10 @@ class TasksDaoTest {
158
152
159
153
// THEN - The loaded data contains the expected values
160
154
val loaded = database.taskDao().getTaskById(task.id)
161
- assertThat(loaded? .id, ` is `(task .id) )
162
- assertThat(loaded? .title, ` is `(task .title) )
163
- assertThat(loaded? .description, ` is `(task .description) )
164
- assertThat( loaded?.isCompleted, ` is `( false ) )
155
+ assertEquals(task .id, loaded? .id)
156
+ assertEquals(task .title, loaded? .title)
157
+ assertEquals(task .description, loaded? .description)
158
+ assertEquals( false , loaded?.isCompleted)
165
159
}
166
160
167
161
@Test
@@ -175,7 +169,7 @@ class TasksDaoTest {
175
169
176
170
// THEN - The list is empty
177
171
val tasks = database.taskDao().getTasks()
178
- assertThat( tasks.isEmpty(), ` is `( true ))
172
+ assertEquals( true , tasks.isEmpty())
179
173
}
180
174
181
175
@Test
@@ -194,7 +188,7 @@ class TasksDaoTest {
194
188
195
189
// THEN - The list is empty
196
190
val tasks = database.taskDao().getTasks()
197
- assertThat( tasks.isEmpty(), ` is `( true ))
191
+ assertEquals( true , tasks.isEmpty())
198
192
}
199
193
200
194
@Test
@@ -209,6 +203,6 @@ class TasksDaoTest {
209
203
210
204
// THEN - The list is empty
211
205
val tasks = database.taskDao().getTasks()
212
- assertThat( tasks.isEmpty(), ` is `( true ))
206
+ assertEquals( true , tasks.isEmpty())
213
207
}
214
208
}
0 commit comments