Sign in Go Pro

Android Clean Architecture

Testing the projects cache data store

This lesson is for PRO members.

Upgrade today to get access to all the PRO lessons.

Unlock this lesson
Autoplay

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)
}

Hey Youssef, it looks like we stub that at line 125 of this class :) If you needed to do something differently though from your side then that would be great to know! Thanks!

Joe can you update the github repo to include the full source code for this project? Some of the files, such as this one are missing.

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.

thanks Steve! The repo is split into branches for each module so I will double check this - if it's needed to be added then I'll stick it in :)

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()
}

}```

>
You need to go PRO to post comments.

Lessons in Android Clean Architecture

Introduction to Clean Architecture
03:40
Joe Birch
GDE
Android and Clean Architecture
03:51
Joe Birch
GDE
Setting up a Multi-Module project
02:24
Joe Birch
GDE
Setting up the Domain layer module
02:24
Joe Birch
GDE
Creating the abstraction for our RxJava Observation Thread
02:02
Joe Birch
GDE
Setting up the Business Data for the Domain Layer
01:47
Joe Birch
GDE
Setting up the data access interface for the Domain Layer
01:49
Joe Birch
GDE
Setting up the Domain Layer Interactor classes
Pro
03:28
Joe Birch
GDE
Implementing the Get Projects use case
Pro
01:49
Joe Birch
GDE
Implementing the get Bookmarked Projects use case
Pro
01:33
Joe Birch
GDE
Implementing the Bookmark Project use case
Pro
01:57
Joe Birch
GDE
Implementing the UnBookmark Project use case (Domain layer 8)
Pro
02:03
Joe Birch
GDE
Testing the Get Projects use case
Pro
02:41
Joe Birch
GDE
Testing the Get Bookmarked Projects use case
Pro
02:04
Joe Birch
GDE
Testing the Bookmark Project use case
Pro
02:10
Joe Birch
GDE
Testing the UnBookmark Project use case
Pro
02:06
Joe Birch
GDE
Setting up the data module
Pro
02:17
Joe Birch
GDE
Creating the data models for the data layer
Pro
02:48
Joe Birch
GDE
Creating the external source interfaces for our data layer
Pro
02:24
Joe Birch
GDE
Creating the data layer store interface
Pro
01:19
Joe Birch
GDE
Creating the data layer cache data store
Pro
01:21
Joe Birch
GDE
Creating the data layer remote data store
Pro
01:39
Joe Birch
GDE
Creating the data store factory class
Pro
01:24
Joe Birch
GDE
Creating the central access point for the data layer
Pro
03:00
Joe Birch
GDE
Setting up the tests for the Data Layer
Pro
00:46
Joe Birch
GDE
Testing the data store factory class
Pro
01:39
Joe Birch
GDE
Testing the project entity mapper
Pro
01:18
Joe Birch
GDE
Testing the projects data repository
Pro
06:02
Joe Birch
GDE
Testing the projects cache data store
Pro
06:30
Joe Birch
GDE
Testing the projects remote data store
Pro
02:17
Joe Birch
GDE
Setting up the remote module
Pro
02:21
Joe Birch
GDE
Creating the data models for the remote layer
Pro
02:06
Joe Birch
GDE
Creating the Retrofit Service for the remote data source
Pro
01:58
Joe Birch
GDE
Creating the Retrofit Service Factory for the remote service interface
Pro
02:52
Joe Birch
GDE
Creating the data model mapper for the Remote Layer
Pro
01:59
Joe Birch
GDE
Creating the Remote Source implementation
Pro
01:55
Joe Birch
GDE
Setting up the Remote layer for testing
Pro
02:13
Joe Birch
GDE
Testing the remote model mapper class
Pro
01:39
Joe Birch
GDE
Testing the remote source implementation
Pro
05:09
Joe Birch
GDE
Setting up the Cache module
Pro
02:45
Joe Birch
GDE
Setting up the Cache layer data models
Pro
02:02
Joe Birch
GDE
Creating the database for the cache layer
Pro
01:34
Joe Birch
GDE
Creating the cached project Data Access Object
Pro
02:42
Joe Birch
GDE
Creating the Configuration Data Access Object
Pro
02:07
Joe Birch
GDE
Creating the cache model mapper
Pro
02:06
Joe Birch
GDE
Creating the Projects Cache data source Implementation
Pro
03:57
Joe Birch
GDE
Setting up the tests for our cache layer
Pro
01:17
Joe Birch
GDE
Testing the cached project model mapper
Pro
01:44
Joe Birch
GDE
Testing the Cached project DAO
Pro
04:33
Joe Birch
GDE
Testing the Configuration DAO
Pro
02:05
Joe Birch
GDE
Testing the projects cache implementation
Pro
05:48
Joe Birch
GDE
Setting up the presentation module
Pro
02:05
Joe Birch
GDE
Creating the data models for the presentation layer
Pro
01:13
Joe Birch
GDE
Creating the data model mapper for the presentation layer
Pro
01:26
Joe Birch
GDE
Creating the presentation state model
Pro
01:39
Joe Birch
GDE
Creating the browse projects view model
Pro
04:25
Joe Birch
GDE
Creating the bookmarked projects view model
Pro
03:05
Joe Birch
GDE
Setting up the presentation module tests
Pro
02:09
Joe Birch
GDE
Testing the view model mapper
Pro
01:17
Joe Birch
GDE
Testing our Browse Projects View Model implementation
Pro
04:36
Joe Birch
GDE
Testing our Browse Bookmarked Projects View Model implementation
Pro
04:34
Joe Birch
GDE
Setting up the UI module
Pro
02:22
Joe Birch
GDE
Creating the Post Execution Thread Implementation
Pro
01:26
Joe Birch
GDE
Setting up the application class
Pro
00:50
Joe Birch
GDE
Setting up the Dependency Injection for the UI module
Pro
02:46
Joe Birch
GDE
Creating the UI modules Project representation
Pro
00:50
Joe Birch
GDE
Creating the the Project model mapper for the UI module
Pro
01:24
Joe Birch
GDE
Setting up the Browse Projects Activity
Pro
01:08
Joe Birch
GDE
Configuring the DI for the project
Pro
04:37
Joe Birch
GDE
Creating the Project item view
Pro
01:51
Joe Birch
GDE
Creating the Browse Projects Adapter
Pro
03:27
Joe Birch
GDE
Creating the browse bookmarked projects adapter
Pro
03:54
Joe Birch
GDE
Creating the browse bookmarked projects activity
Pro
03:18
Joe Birch
GDE
Tying together the browse projects and bookmarked projects activity
Pro
02:30
Joe Birch
GDE
Setting up Unit Tests for the UI module
Pro
00:54
Joe Birch
GDE
Test the project model mapper
Pro
01:10
Joe Birch
GDE
Configuring the Dependency Injection for our User Interface tests
Pro
06:10
Joe Birch
GDE
Writing Espresso tests for the Browse Projects Activity
Pro
03:15
Joe Birch
GDE
Writing Espresso tests for the Bookmarked Projects Activity
Pro
02:34
Joe Birch
GDE
Simplifying the work we have done
Pro
05:14
Joe Birch
GDE