Skip to content

Commit 385ec18

Browse files
authored
feat: Adding simple sample instrumentation test for ShopViewModel (#151)
1 parent 01c5b6c commit 385ec18

File tree

4 files changed

+76
-51
lines changed

4 files changed

+76
-51
lines changed

core-sdk-samples/higgs-shop-sample-app/app/build.gradle.kts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ dependencies {
117117
androidTestImplementation("androidx.test:runner:1.4.0")
118118
androidTestImplementation("com.google.truth:truth:1.1.3")
119119
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
120+
androidTestImplementation("androidx.arch.core:core-testing:2.1.0")
121+
androidTestImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4")
122+
androidTestImplementation("org.mockito.kotlin:mockito-kotlin:4.0.0")
120123

121124
androidTestUtil("androidx.test:orchestrator:1.4.1")
122125
}

core-sdk-samples/higgs-shop-sample-app/app/src/androidTest/kotlin/com/mparticle/example/higgsshopsampleapp/ExampleInstrumentedTests.kt

Lines changed: 0 additions & 51 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.mparticle.example.higgsshopsampleapp
2+
3+
import androidx.lifecycle.LiveData
4+
import androidx.lifecycle.Observer
5+
import kotlin.coroutines.resume
6+
import kotlin.coroutines.suspendCoroutine
7+
8+
/**
9+
* Get value from LiveData using suspend coroutine
10+
*/
11+
suspend fun <T> LiveData<T?>.getValueFromLiveData() = suspendCoroutine<T?> {
12+
var observer: Observer<T?>? = null
13+
observer = Observer<T?> { t ->
14+
observer?.let { this.removeObserver(it) }
15+
it.resume(t)
16+
}
17+
this.observeForever(observer)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.mparticle.example.higgsshopsampleapp
2+
3+
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+
import androidx.test.core.app.ApplicationProvider
5+
import androidx.test.internal.runner.junit4.AndroidJUnit4ClassRunner
6+
import com.mparticle.example.higgsshopsampleapp.repositories.CartRepository
7+
import com.mparticle.example.higgsshopsampleapp.repositories.ProductsRepository
8+
import com.mparticle.example.higgsshopsampleapp.viewmodels.ShopViewModel
9+
import kotlinx.coroutines.Dispatchers
10+
import kotlinx.coroutines.test.StandardTestDispatcher
11+
import kotlinx.coroutines.test.resetMain
12+
import kotlinx.coroutines.test.runTest
13+
import kotlinx.coroutines.test.setMain
14+
import org.junit.*
15+
import org.junit.runner.RunWith
16+
17+
@RunWith(AndroidJUnit4ClassRunner::class)
18+
class TestShopViewModel {
19+
20+
@get:Rule
21+
val instantExecutorRule = InstantTaskExecutorRule()
22+
private val dispatcher = StandardTestDispatcher()
23+
24+
private val productsRepository = ProductsRepository()
25+
private val cartRepository = CartRepository()
26+
private val viewModel = ShopViewModel()
27+
28+
@Before
29+
fun setup() {
30+
Dispatchers.setMain(dispatcher)
31+
}
32+
33+
@After
34+
fun tearDown() {
35+
Dispatchers.resetMain()
36+
}
37+
38+
@Test
39+
fun `testGetProducts`() = runTest {
40+
val products = productsRepository.getProducts(ApplicationProvider.getApplicationContext())
41+
viewModel.getProducts(ApplicationProvider.getApplicationContext())
42+
Assert.assertEquals(products, viewModel.inventoryResponseLiveData.getValueFromLiveData())
43+
}
44+
45+
@Test
46+
fun `testGetTotalCartItems`() = runTest {
47+
val cartItems = cartRepository.getCartItems(ApplicationProvider.getApplicationContext())
48+
viewModel.getTotalCartItems(ApplicationProvider.getApplicationContext())
49+
Assert.assertEquals(
50+
cartItems.sumOf { it.quantity },
51+
viewModel.cartTotalSizeResponseLiveData.getValueFromLiveData()
52+
)
53+
}
54+
55+
}

0 commit comments

Comments
 (0)