Skip to content

Commit

Permalink
feat: Using cashapp/turbine for more robust flow tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vipulshah2010 committed Aug 4, 2020
1 parent 23916d2 commit c8a45b0
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 28 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ buildscript {
repositories {
google()
jcenter()
mavenCentral()
}
}

allprojects {
repositories {
google()
jcenter()
mavenCentral()
maven {
url = uri("https://jitpack.io")
}
Expand Down
2 changes: 2 additions & 0 deletions buildSrc/src/main/kotlin/Deps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object Deps {
private const val testEspressoVersion = "3.2.0"
private const val testJU5Version = "5.6.2"
private const val truthVersion = "1.0.1"
private const val turbineVersion = "0.1.1"

/********************** Dependencies **********************/

Expand Down Expand Up @@ -103,5 +104,6 @@ object Deps {
const val uiAutomator = "androidx.test.uiautomator:uiautomator:2.2.0"
const val roomTesting = "androidx.room:room-testing:$roomVersion"
const val truth = "com.google.truth:truth:$truthVersion"
const val turbine = "app.cash.turbine:turbine:$turbineVersion"

}
35 changes: 23 additions & 12 deletions buildSrc/src/main/kotlin/FoodAppPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,12 @@ internal fun Project.configureCommonDependencies() {

extensions.getByType<BaseExtension>().run {
dependencies {

add("kapt", Deps.hiltCompiler)
add("kapt", Deps.lifecycleCompiler)
add("kapt", Deps.hiltViewModelCompiler)
add("kapt", Deps.roomCompiler)

add("implementation", Deps.coroutine)
add("implementation", Deps.coroutineCore)
add("implementation", Deps.coroutineTest)
Expand All @@ -111,7 +117,7 @@ internal fun Project.configureCommonDependencies() {
add("implementation", Deps.fragment)
add("implementation", Deps.liveData)
add("implementation", Deps.lifecycleCommon)
add("kapt", Deps.lifecycleCompiler)

add("implementation", Deps.lifecycleExt)
add("implementation", Deps.lifecycleViewModel)
add("implementation", Deps.navigationFragment)
Expand All @@ -126,36 +132,41 @@ internal fun Project.configureCommonDependencies() {
add("implementation", Deps.commonsLang)
add("implementation", Deps.gson)
add("implementation", Deps.hilt)
add("kapt", Deps.hiltCompiler)
add("implementation", Deps.hiltViewModel)
add("kapt", Deps.hiltViewModelCompiler)

add("implementation", Deps.room)
add("implementation", Deps.roomKtx)
add("kapt", Deps.roomCompiler)

add("implementation", Deps.retrofit)
add("implementation", Deps.converter)
add("implementation", Deps.retrofitInterceptor)
add("implementation", Deps.coil)

add("debugImplementation", Deps.fragmentTesting)
add("testImplementation", Deps.archCoreTesting)
add("testImplementation", Deps.roomTesting)
add("testImplementation", Deps.truth)

add("implementation", Deps.testCoreDep)

add("androidTestImplementation", Deps.espresso)
add("androidTestImplementation", Deps.espressoContrib)
add("androidTestImplementation", Deps.espressoIdling)
add("androidTestImplementation", Deps.espressoIntents)
add("testImplementation", Deps.jU5api)
add("testRuntimeOnly", Deps.jU5Engine)
add("androidTestImplementation", Deps.jU5TestCore)
add("androidTestRuntimeOnly", Deps.jU5TestRunner)
add("androidTestImplementation", Deps.jUnitExt)
add("testImplementation", Deps.mockk)
add("androidTestUtil", Deps.orchestrator)
add("androidTestImplementation", Deps.rules)
add("androidTestImplementation", Deps.runner)
add("androidTestImplementation", Deps.uiAutomator)

add("testRuntimeOnly", Deps.jU5Engine)
add("testRuntimeOnly", Deps.jU5Engine)
add("androidTestRuntimeOnly", Deps.jU5TestRunner)
add("androidTestUtil", Deps.orchestrator)

add("testImplementation", Deps.archCoreTesting)
add("testImplementation", Deps.roomTesting)
add("testImplementation", Deps.truth)
add("testImplementation", Deps.mockk)
add("testImplementation", Deps.jU5api)
add("testImplementation", Deps.turbine)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package foodapp.com.data.repository

import app.cash.turbine.test
import com.google.common.truth.Truth
import foodapp.com.data.FoodDatabase
import foodapp.com.data.FoodResult
Expand All @@ -17,16 +18,17 @@ import io.mockk.*
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.test.TestCoroutineDispatcher
import kotlinx.coroutines.test.TestCoroutineScope
import kotlinx.coroutines.test.runBlockingTest
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.TestInstance
import org.junit.jupiter.api.extension.ExtendWith
import kotlin.time.ExperimentalTime

@ExperimentalCoroutinesApi
@ExperimentalTime
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@ExtendWith(MockKExtension::class)
internal class FoodRepositoryTest : CoroutineTest {
Expand Down Expand Up @@ -64,9 +66,10 @@ internal class FoodRepositoryTest : CoroutineTest {
coEvery { restApi.getFoodItems() } returns MockData.foodItems
every { foodDao.insertAll(capture(foodItemsSlot)) } just runs

repository.getFoodItems(true).collect {
Truth.assertThat(it).isInstanceOf(FoodResult.Success::class.java)
repository.getFoodItems(true).test {
Truth.assertThat(expectItem()).isInstanceOf(FoodResult.Success::class.java)
Truth.assertThat(foodItemsSlot.captured.size).isEqualTo(3)
expectComplete()
}
}

Expand All @@ -76,9 +79,11 @@ internal class FoodRepositoryTest : CoroutineTest {
coEvery { foodDao.allFoodItems } returns MockData.foodItems.fooditems
every { foodDao.insertAll(capture(foodItemsSlot)) } just runs

repository.getFoodItems(false).collect {
Truth.assertThat(it).isInstanceOf(FoodResult.Success::class.java)
Truth.assertThat((it as FoodResult.Success).data).hasSize(3)
repository.getFoodItems(false).test {
val item = expectItem()
Truth.assertThat(item).isInstanceOf(FoodResult.Success::class.java)
Truth.assertThat((item as FoodResult.Success).data).hasSize(3)
expectComplete()
}
}

Expand All @@ -89,9 +94,10 @@ internal class FoodRepositoryTest : CoroutineTest {
coEvery { foodDao.allFoodItems } returns MockData.emptyFoodIItems.fooditems
every { foodDao.insertAll(capture(foodItemsSlot)) } just runs

repository.getFoodItems(false).collect {
Truth.assertThat(it).isInstanceOf(FoodResult.Success::class.java)
repository.getFoodItems(false).test {
Truth.assertThat(expectItem()).isInstanceOf(FoodResult.Success::class.java)
Truth.assertThat(foodItemsSlot.captured.size).isEqualTo(3)
expectComplete()
}
}

Expand All @@ -100,9 +106,10 @@ internal class FoodRepositoryTest : CoroutineTest {
val foodItemIdSlot = slot<Int>()
every { foodDao.getFoodItem(capture(foodItemIdSlot)) } returns MockData.foodItem

repository.getFoodItem(1).collect {
Truth.assertThat(it).isInstanceOf(FoodResult.Success::class.java)
repository.getFoodItem(1).test {
Truth.assertThat(expectItem()).isInstanceOf(FoodResult.Success::class.java)
Truth.assertThat(foodItemIdSlot.captured).isEqualTo(1)
expectComplete()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ internal class FoodUsecaseTest : CoroutineTest {
override lateinit var dispatcher: TestCoroutineDispatcher
override lateinit var testScope: TestCoroutineScope

@MockK
@MockK(relaxed = true)
lateinit var localDataStore: LocalDataStore

@MockK
@MockK(relaxed = true)
lateinit var remoteCloudDataStore: CloudDataStore

private lateinit var repository: FoodRepository
Expand All @@ -49,10 +49,6 @@ internal class FoodUsecaseTest : CoroutineTest {

@Test
fun `Test usecase getFoodItems request calls repository`() = dispatcher.runBlockingTest {
every { repository.getFoodItems(true) } answers {
flow { emit(MockFactory.generateFoodItems(exception = false)) }
}

val usecase = FoodUsecase(repository)
usecase.getFoodItems(true)
verify(exactly = 1) { repository.getFoodItems(true) }
Expand Down

0 comments on commit c8a45b0

Please sign in to comment.