Skip to content

Commit 1b38f42

Browse files
committed
test: add unit tests
1 parent 4bb07bc commit 1b38f42

File tree

4 files changed

+84
-19
lines changed

4 files changed

+84
-19
lines changed

app/build.gradle

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ android {
3333
buildFeatures {
3434
viewBinding true
3535
}
36+
testOptions {
37+
unitTests {
38+
includeAndroidResources = true
39+
returnDefaultValues = true
40+
}
41+
}
3642
}
3743

3844
dependencies {
@@ -46,6 +52,8 @@ dependencies {
4652
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.1'
4753
implementation 'androidx.navigation:navigation-fragment-ktx:2.5.3'
4854
implementation 'androidx.navigation:navigation-ui-ktx:2.5.3'
55+
56+
testImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.6.4'
4957
testImplementation 'junit:junit:4.13.2'
5058
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
5159
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'

app/src/main/java/com/hoc081988/democoroutineschannelresult/MainVM.kt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.hoc081988.democoroutineschannelresult
22

33
import android.util.Log
4+
import androidx.annotation.VisibleForTesting
45
import androidx.lifecycle.ViewModel
56
import kotlinx.coroutines.channels.Channel
67
import kotlinx.coroutines.flow.Flow
@@ -45,7 +46,7 @@ class MainVM : ViewModel() {
4546
private val eventChannels: Map<MainSingleEvent.Key<SomeMainSingleEvent>, Channel<SomeMainSingleEvent>> =
4647
MainSingleEvent.KEYS.associateBy(
4748
keySelector = { it },
48-
valueTransform = { Channel<MainSingleEvent<SomeMainSingleEvent>>(Channel.UNLIMITED) }
49+
valueTransform = { Channel(Channel.UNLIMITED) }
4950
)
5051

5152
fun <T : MainSingleEvent<T>> sendEvent(event: T) {
@@ -61,7 +62,8 @@ class MainVM : ViewModel() {
6162
.receiveAsFlow()
6263
.map { it as T }
6364

64-
override fun onCleared() {
65+
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
66+
public override fun onCleared() {
6567
super.onCleared()
6668
eventChannels.values.forEach { it.close() }
6769
}

app/src/test/java/com/hoc081988/democoroutineschannelresult/ExampleUnitTest.kt

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.hoc081988.democoroutineschannelresult
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.ExperimentalCoroutinesApi
5+
import kotlinx.coroutines.delay
6+
import kotlinx.coroutines.flow.merge
7+
import kotlinx.coroutines.flow.take
8+
import kotlinx.coroutines.flow.toList
9+
import kotlinx.coroutines.test.StandardTestDispatcher
10+
import kotlinx.coroutines.test.resetMain
11+
import kotlinx.coroutines.test.runTest
12+
import kotlinx.coroutines.test.setMain
13+
import org.junit.After
14+
import org.junit.Assert
15+
import org.junit.Before
16+
import org.junit.Test
17+
18+
@ExperimentalCoroutinesApi
19+
class MainVMTest {
20+
private val testDispatcher = StandardTestDispatcher()
21+
22+
@Before
23+
fun setUp() {
24+
Dispatchers.setMain(testDispatcher)
25+
}
26+
27+
@After
28+
fun tearDown() {
29+
Dispatchers.resetMain()
30+
}
31+
32+
@Test
33+
fun bufferEvents() = runTest(testDispatcher) {
34+
val vm = MainVM()
35+
36+
val originalEvents = (0..50).map {
37+
val text = it.toString()
38+
39+
when (it % 3) {
40+
0 -> MainSingleEvent.HomeFragmentResult(text)
41+
1 -> MainSingleEvent.DashboardFragmentResult(text)
42+
else -> MainSingleEvent.HomeDetailsResult(text)
43+
}
44+
}
45+
originalEvents.forEach(vm::sendEvent)
46+
delay(100)
47+
48+
val actualEvents = MainSingleEvent.KEYS.map { vm.receiveEventFlow(it) }
49+
.merge()
50+
.take(originalEvents.size)
51+
.toList()
52+
53+
Assert.assertEquals(
54+
originalEvents.sortedBy { it.hashCode() },
55+
actualEvents.sortedBy { it.hashCode() }
56+
)
57+
}
58+
59+
@Test
60+
fun throwsAfterCleared() = runTest(testDispatcher) {
61+
val vm = MainVM()
62+
vm.onCleared()
63+
64+
Assert.assertThrows(Exception::class.java) {
65+
vm.sendEvent(MainSingleEvent.HomeFragmentResult("1"))
66+
}
67+
68+
vm.receiveEventFlow(MainSingleEvent.HomeFragmentResult)
69+
.toList()
70+
.let { Assert.assertTrue(it.isEmpty()) }
71+
}
72+
}

0 commit comments

Comments
 (0)