Skip to content
Permalink
Browse files

Migrate to MockK

  • Loading branch information...
chrisbanes committed Jul 7, 2019
1 parent a8fd96b commit 582e570f98030efe0cb0b69e35bcd63cf93df997
@@ -233,7 +233,6 @@ dependencies {

testImplementation Libs.junit
testImplementation Libs.robolectric
testImplementation Libs.mockitoKotlin
testImplementation Libs.AndroidX.Test.core
testImplementation Libs.AndroidX.Test.runner
testImplementation Libs.AndroidX.Test.rules
@@ -44,7 +44,7 @@ object Libs {

const val junit = "junit:junit:4.12"
const val robolectric = "org.robolectric:robolectric:4.3"
const val mockitoKotlin = "com.nhaarman.mockitokotlin2:mockito-kotlin:2.1.0"
const val mockK = "io.mockk:mockk:1.9.3"

object Google {
const val material = "com.google.android.material:material:1.1.0-alpha07"
@@ -75,7 +75,7 @@ dependencies {

testImplementation Libs.junit
testImplementation Libs.robolectric
testImplementation Libs.mockitoKotlin
testImplementation Libs.mockK

testImplementation Libs.AndroidX.archCoreTesting
testImplementation Libs.AndroidX.Test.core
@@ -35,7 +35,6 @@ import org.junit.Assert.assertThat
import org.junit.Ignore
import org.junit.Test

@Ignore("https://github.com/robolectric/robolectric/issues/3556")
class EpisodeWatchEntryTest : BaseDatabaseTest() {
private lateinit var episodeWatchEntryDao: EpisodeWatchEntryDao

@@ -31,7 +31,6 @@ import org.junit.Assert.assertThat
import org.junit.Ignore
import org.junit.Test

@Ignore("https://github.com/robolectric/robolectric/issues/3556")
class EpisodesTest : BaseDatabaseTest() {
private lateinit var episodeDao: EpisodesDao

@@ -33,7 +33,6 @@ import org.junit.Assert.assertThat
import org.junit.Ignore
import org.junit.Test

@Ignore("https://github.com/robolectric/robolectric/issues/3556")
class SeasonsTest : BaseDatabaseTest() {
private lateinit var seasonsDao: SeasonsDao

@@ -20,11 +20,11 @@ import app.tivi.data.daos.EntityInserter
import app.tivi.data.daos.FollowedShowsDao
import app.tivi.data.entities.Success
import app.tivi.data.repositories.followedshows.FollowedShowsDataSource
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
import app.tivi.data.repositories.followedshows.FollowedShowsLastRequestStore
import app.tivi.data.repositories.followedshows.FollowedShowsRepository
import app.tivi.data.repositories.followedshows.FollowedShowsStore
import app.tivi.data.repositories.shows.ShowStore
import app.tivi.data.repositories.shows.ShowRepository
import app.tivi.data.repositories.shows.ShowStore
import app.tivi.trakt.TraktAuthState
import app.tivi.util.Logger
import app.tivi.utils.BaseDatabaseTest
@@ -40,14 +40,12 @@ import app.tivi.utils.show
import app.tivi.utils.show2
import app.tivi.utils.show2Id
import app.tivi.utils.showId
import io.mockk.coEvery
import io.mockk.coVerify
import io.mockk.mockk
import org.hamcrest.Matchers.`is`
import org.junit.Assert.assertThat
import org.junit.Test
import org.mockito.ArgumentMatchers.any
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import org.mockito.Mockito.times
import org.mockito.Mockito.verify
import javax.inject.Provider

class FollowedShowRepositoryTest : BaseDatabaseTest() {
@@ -67,13 +65,13 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {

followShowsDao = db.followedShowsDao()

showRepository = mock(ShowRepository::class.java)
`when`(showRepository.needsUpdate(any(Long::class.java))).thenReturn(true)
showRepository = mockk()
coEvery { showRepository.needsUpdate(any()) } returns true

val logger = mock(Logger::class.java)
val logger = mockk<Logger>(relaxUnitFun = true)
val txRunner = TestTransactionRunner
val entityInserter = EntityInserter(txRunner, logger)
traktDataSource = mock(FollowedShowsDataSource::class.java)
traktDataSource = mockk()

repository = FollowedShowsRepository(
FollowedShowsStore(txRunner, entityInserter, db.followedShowsDao(), db.showDao(), logger),
@@ -89,21 +87,18 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {

@Test
fun testSync() = runBlockingTest {
`when`(traktDataSource.getFollowedListId()).thenReturn(0)
`when`(traktDataSource.getListShows(0)).thenReturn(Success(listOf(followedShow1 to show)))
coEvery { traktDataSource.getFollowedListId() } returns 0
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow1 to show))

assertThat(repository.getFollowedShows(), `is`(listOf(followedShow1)))

// Verify that a show update was triggered
verify(showRepository, times(1)).updateShow(showId)
}

@Test
fun testSync_emptyResponse() = runBlockingTest {
insertFollowedShow(db)

`when`(traktDataSource.getFollowedListId()).thenReturn(0)
`when`(traktDataSource.getListShows(0)).thenReturn(Success(emptyList()))
coEvery { traktDataSource.getFollowedListId() } returns 0
coEvery { traktDataSource.getListShows(0) } returns Success(emptyList())

assertThat(repository.getFollowedShows(), `is`(emptyList()))
}
@@ -112,21 +107,18 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
fun testSync_responseDifferentShow() = runBlockingTest {
insertFollowedShow(db)

`when`(traktDataSource.getFollowedListId()).thenReturn(0)
`when`(traktDataSource.getListShows(0)).thenReturn(Success(listOf(followedShow2 to show2)))
coEvery { traktDataSource.getFollowedListId()} returns 0
coEvery { traktDataSource.getListShows(0) } returns Success(listOf(followedShow2 to show2))

assertThat(repository.getFollowedShows(), `is`(listOf(followedShow2)))

// Verify that a show update was triggered
verify(showRepository, times(1)).updateShow(show2Id)
}

@Test
fun testSync_pendingDelete() = runBlockingTest {
followShowsDao.insert(followedShow1PendingDelete)

// Return null for the list ID so that we disable syncing
`when`(traktDataSource.getFollowedListId()).thenReturn(null)
coEvery { traktDataSource.getFollowedListId()} returns null

assertThat(repository.getFollowedShows(), `is`(emptyList()))
}
@@ -136,7 +128,7 @@ class FollowedShowRepositoryTest : BaseDatabaseTest() {
followShowsDao.insert(followedShow1PendingUpload)

// Return null for the list ID so that we disable syncing
`when`(traktDataSource.getFollowedListId()).thenReturn(null)
coEvery { traktDataSource.getFollowedListId()} returns null

assertThat(repository.getFollowedShows(), `is`(listOf(followedShow1)))
}
@@ -46,12 +46,12 @@ import app.tivi.utils.s2_id
import app.tivi.utils.s2e1
import app.tivi.utils.showId
import app.tivi.utils.testCoroutineDispatchers
import io.mockk.coEvery
import io.mockk.mockk
import kotlinx.coroutines.runBlocking
import org.hamcrest.Matchers.`is`
import org.junit.Assert.assertThat
import org.junit.Test
import org.mockito.Mockito.`when`
import org.mockito.Mockito.mock
import javax.inject.Provider

class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
@@ -77,11 +77,11 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
episodesDao = db.episodesDao()
seasonsDao = db.seasonsDao()

traktSeasonsDataSource = mock(SeasonsEpisodesDataSource::class.java)
traktEpisodeDataSource = mock(EpisodeDataSource::class.java)
tmdbEpisodeDataSource = mock(EpisodeDataSource::class.java)
traktSeasonsDataSource = mockk()
traktEpisodeDataSource = mockk()
tmdbEpisodeDataSource = mockk()

val logger = mock(Logger::class.java)
val logger = mockk<Logger>(relaxUnitFun = true)
val txRunner = TestTransactionRunner
val entityInserter = EntityInserter(txRunner, logger)

@@ -113,9 +113,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
db.episodesDao().insertAll(s1_episodes)

// Return a response with 2 items
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId)).thenReturn(
coEvery { traktSeasonsDataSource.getShowEpisodeWatches(showId) } returns
Success(listOf(s1e1 to s1e1w, s1e1 to s1e1w2))
)
// Sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that both are in the db
@@ -130,8 +129,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a response with the same items
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId))
.thenReturn(Success(listOf(s1e1 to s1e1w, s1e1 to s1e1w2)))
coEvery { traktSeasonsDataSource.getShowEpisodeWatches(showId) } returns
Success (listOf(s1e1 to s1e1w, s1e1 to s1e1w2))
// Now re-sync with the same response
repository.syncEpisodeWatchesForShow(showId)
// Assert that both are in the db
@@ -146,8 +145,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a response with just the second item
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId))
.thenReturn(Success(listOf(s1e1 to s1e1w2)))
coEvery { traktSeasonsDataSource.getShowEpisodeWatches(showId) } returns
Success(listOf(s1e1 to s1e1w2))
// Now re-sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that only the second is in the db
@@ -162,7 +161,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
// Insert both the watches
episodeWatchDao.insertAll(s1e1w, s1e1w2)
// Return a empty response
`when`(traktSeasonsDataSource.getShowEpisodeWatches(showId)).thenReturn(Success(emptyList()))
coEvery { traktSeasonsDataSource.getShowEpisodeWatches(showId) } returns
Success(emptyList())
// Now re-sync
repository.syncEpisodeWatchesForShow(showId)
// Assert that the database is empty
@@ -172,8 +172,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
@Test
fun testSyncSeasonsEpisodes() = runBlockingTest {
// Return a response with 2 items
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
coEvery { traktSeasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(listOf(s1 to s1_episodes))
repository.updateSeasonsEpisodes(showId)

// Assert that both are in the db
@@ -187,8 +187,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
db.episodesDao().insertAll(s1_episodes)

// Return a response with the same items
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
coEvery { traktSeasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(listOf(s1 to s1_episodes))
repository.updateSeasonsEpisodes(showId)

// Assert that both are in the db
@@ -202,7 +202,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
db.episodesDao().insertAll(s1_episodes)

// Return an empty response
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId)).thenReturn(Success(emptyList()))
coEvery { traktSeasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(emptyList())
repository.updateSeasonsEpisodes(showId)

// Assert the database is empty
@@ -217,8 +218,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
db.episodesDao().insertAll(s2_episodes)

// Return a response with just the first season
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to s1_episodes)))
coEvery { traktSeasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(listOf(s1 to s1_episodes))
repository.updateSeasonsEpisodes(showId)

// Assert that both are in the db
@@ -233,8 +234,8 @@ class SeasonsEpisodesRepositoryTest : BaseDatabaseTest() {
db.episodesDao().insertAll(s2_episodes)

// Return a response with both seasons, but just a single episodes in each
`when`(traktSeasonsDataSource.getSeasonsEpisodes(showId))
.thenReturn(Success(listOf(s1 to listOf(s1e1), s2 to listOf(s2e1))))
coEvery { traktSeasonsDataSource.getSeasonsEpisodes(showId) } returns
Success(listOf(s1 to listOf(s1e1), s2 to listOf(s2e1)))
repository.updateSeasonsEpisodes(showId)

// Assert that both are in the db

0 comments on commit 582e570

Please sign in to comment.
You can’t perform that action at this time.