Android Clean Architecture
Testing the projects cache data store
Up next
Previous
About
The Data layer uses a data store class to handle the data layer level communication with the cache data source. In this lesson, we'll be creating tests to ensure that the cache data store class remains functional as intended over time.
Instructor
Links
Comments
Hey,
first thank you so much for this amazing course.
I believe to make the tests run there should be a stub for cache.setLastCacheTime
Something like that
private fun stubSaveVendors(completable: Completable) {
stubsetLastCacheTime(completable)
whenever(cache.saveVendors(any()))
.thenReturn(completable)
}
private fun stubsetLastCacheTime(completable: Completable) {
whenever(cache.setLastCacheTime(any()))
.thenReturn(completable)
}
Heads up to anyone following along this course. Once you get to 2 minutes into the video you'll create a test called saveProjectsCompletes this test will fail with a:
"java.lang.NullPointerException: other is null" error. This is happening because after the cache runs saveProjects it then attempts to run the setLastCacheTime method. You need to stub this method as Youssef mentions above. Unfortunately the source files are NOT available, but if you watch the video closely you'll see that Joe does indeed stub this method. I think in the editing of the videos it gets lost. At time 2:49 you can see the phantom stub method we need. My method is:
private fun stubCacheSetLastCacheTime(completable: Completable){
whenever(cache.setLastCacheTime(any()))
.thenReturn(completable)
}
...and as you can see (at time 2:49) in Joe's actual working method he included this stub method.
stubCacheSetLastCacheTime(Completable.complete())
now all 14 tests will complete successfully.
Here is my version of ProjectsCachDataStoreTest
using KStubbing. It's more compact I think.
```java
class ProjectsCacheDataStoreTest {
private val projectEntity = ProjectFactory.makeProjectEntity()
private val projectId = DataFactory.randomString()
@Mock
private lateinit var projectsCacheDataStore: ProjectsCacheDataStore
private val projectCache = mock() {
on {
getProjects()
}.thenReturn(Observable.just(listOf(projectEntity)))
on {
saveProjects(listOf(projectEntity))
}.thenReturn(Completable.complete())
on {
getBookmarkedProjects()
}.thenReturn(Observable.just(listOf(projectEntity)))
on {
bookmarkedProject(projectId)
}.thenReturn(Completable.complete())
on {
unBookmarkedProject(projectId)
}.thenReturn(Completable.complete())
on {
setLastCacheTime(any())
}.thenReturn(Completable.complete())
}
@Before
fun setup() {
projectsCacheDataStore = ProjectsCacheDataStore(projectsCache = projectCache)
}
@Test
fun getProjectsCompletes() {
val testObserver = projectsCacheDataStore.getProjects().test()
testObserver.assertComplete()
}
@Test
fun getProjectsReturnData() {
val testObserver = projectsCacheDataStore.getProjects().test()
testObserver.assertValue(listOf(projectEntity))
}
@Test
fun saveProjectsCompletes() {
val testObserver = projectsCacheDataStore.saveProjects(listOf(projectEntity)).test()
testObserver.assertComplete()
}
@Test
fun getProjectsBookmarkedCompletes() {
val testObserver = projectsCacheDataStore.getBookmarkedProjects().test()
testObserver.assertComplete()
}
@Test
fun bookmarkProjectsCompletes() {
val testObserver = projectsCacheDataStore.bookmarkedProject(projectId).test()
testObserver.assertComplete()
}
@Test
fun unbookMarkedCompletes() {
val testObserver = projectsCacheDataStore.unBookmarkedProject(projectId).test()
testObserver.assertComplete()
}
}```
Lessons in Android Clean Architecture















































































