Skip to content

Commit 04f7714

Browse files
Add coroutines tests
1 parent 1fc8658 commit 04f7714

File tree

4 files changed

+118
-26
lines changed

4 files changed

+118
-26
lines changed

Tutorial4-2Kotlin-MockK/src/main/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/viewmodel/CalculationUseCase.kt

Lines changed: 0 additions & 13 deletions
This file was deleted.

Tutorial4-2Kotlin-MockK/src/main/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/viewmodel/CalculationViewModel.kt

Lines changed: 0 additions & 6 deletions
This file was deleted.

Tutorial4-2Kotlin-MockK/src/test/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/Test16Coroutines.kt

Lines changed: 111 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,28 @@
11
package com.smarttoolfactory.tutorial4_2kotlin_mockk
22

33
import com.google.common.truth.Truth
4-
import kotlinx.coroutines.GlobalScope
5-
import kotlinx.coroutines.async
6-
import kotlinx.coroutines.delay
7-
import kotlinx.coroutines.runBlocking
4+
import kotlinx.coroutines.*
5+
import kotlinx.coroutines.test.TestCoroutineDispatcher
6+
import kotlinx.coroutines.test.resetMain
7+
import kotlinx.coroutines.test.runBlockingTest
8+
import kotlinx.coroutines.test.setMain
9+
import org.junit.After
10+
import org.junit.Before
811
import org.junit.jupiter.api.Test
912

13+
@ExperimentalCoroutinesApi
1014
class Test16Coroutines {
1115

16+
private val testCoroutineDispatcher = TestCoroutineDispatcher()
17+
18+
19+
/**
20+
* This test passes but it has to WAIT the delay of suspending function
21+
*/
1222
@Test
13-
fun `First Coroutine test`()= runBlocking {
23+
fun `First Coroutine test`() = runBlocking {
24+
25+
println("Test started")
1426

1527
// GIVEN
1628
val expected = "Hello World"
@@ -22,15 +34,108 @@ class Test16Coroutines {
2234

2335
// THEN
2436
Truth.assertThat(actual.await()).isEqualTo(expected)
37+
println("Test finished")
38+
39+
}
40+
41+
42+
/**
43+
This test FAILS because it's not [TestCoroutineDispatcher]
44+
that calling [TestCoroutineDispatcher.runBlockingTest]
45+
*/
46+
@Test
47+
fun `Coroutine test with runBlockingTest that fails`() = runBlockingTest {
48+
49+
println("Test started")
50+
51+
println("Test thread: ${Thread.currentThread().name}, scope: $this")
52+
53+
// GIVEN
54+
val expected = "Hello World"
55+
56+
// WHEN
57+
val actual = withContext(testCoroutineDispatcher) {
58+
println("Test Dispatcher thread: ${Thread.currentThread().name}, scope: $this")
59+
60+
async {
61+
println("Inside async thread: ${Thread.currentThread().name}, scope: $this")
62+
63+
mockSomeDelayedWork()
64+
}
65+
}
66+
67+
// THEN
68+
Truth.assertThat(actual.await()).isEqualTo(expected)
69+
println("Test finished")
70+
71+
/*
72+
Prints:
73+
Test started
74+
Test thread: main @coroutine#1, scope: kotlinx.coroutines.test.TestCoroutineScopeImpl@157632c9
75+
Test Dispatcher thread: main @coroutine#1, scope: "coroutine#1":DispatchedCoroutine{Active}@67e2d983
76+
Inside async thread: main @coroutine#2, scope: "coroutine#2":DeferredCoroutine{Active}@5d47c63f
77+
*/
78+
79+
}
80+
81+
@Test
82+
fun `Coroutine test with TestCoroutineDispatcher`() = testCoroutineDispatcher.runBlockingTest {
83+
84+
println("Test started")
85+
86+
println("Test thread: ${Thread.currentThread().name}, scope: $this")
87+
88+
// GIVEN
89+
val expected = "Hello World"
90+
91+
// WHEN
92+
val actual = withContext(testCoroutineDispatcher) {
93+
println("Test Dispatcher thread: ${Thread.currentThread().name}, scope: $this")
94+
95+
async {
96+
println("Inside async thread: ${Thread.currentThread().name}, scope: $this")
97+
98+
mockSomeDelayedWork()
99+
}
100+
}
101+
102+
// THEN
103+
Truth.assertThat(actual.await()).isEqualTo(expected)
104+
println("Test finished")
105+
106+
/*
107+
Prints:
108+
109+
Test started
110+
Test thread: main @coroutine#1, scope: kotlinx.coroutines.test.TestCoroutineScopeImpl@157632c9
111+
Test Dispatcher thread: main @coroutine#1, scope: "coroutine#1":UndispatchedCoroutine{Active}@3bbc39f8
112+
Inside async thread: main @coroutine#2, scope: "coroutine#2":DeferredCoroutine{Active}@4ae3c1cd
113+
Test finished
114+
*/
115+
116+
}
117+
118+
@Before
119+
fun setUp() {
120+
Dispatchers.setMain(testCoroutineDispatcher)
25121
}
26122

123+
@After
124+
fun tearDown() {
125+
Dispatchers.resetMain()
126+
try {
127+
testCoroutineDispatcher.cleanupTestCoroutines()
128+
} catch (e: Exception) {
129+
e.printStackTrace()
130+
}
131+
}
27132

28133
}
29134

30135

31136
private suspend fun mockSomeDelayedWork(): String {
32137

33-
delay(10_000)
138+
delay(5_000)
34139
return "Hello World"
35140

36141
}

Tutorial4-2Kotlin-MockK/src/test/java/com/smarttoolfactory/tutorial4_2kotlin_mockk/TestCoroutineRule.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,18 @@ class TestCoroutineRule : TestRule {
1717
override fun apply(base: Statement, description: Description?) = object : Statement() {
1818
@Throws(Throwable::class)
1919
override fun evaluate() {
20+
2021
Dispatchers.setMain(testCoroutineDispatcher)
2122

2223
base.evaluate()
2324

2425
Dispatchers.resetMain()
25-
testCoroutineScope.cleanupTestCoroutines()
26+
27+
try {
28+
testCoroutineScope.cleanupTestCoroutines()
29+
} catch (e: Exception) {
30+
e.printStackTrace()
31+
}
2632
}
2733
}
2834

0 commit comments

Comments
 (0)