11package  com.smarttoolfactory.tutorial4_2kotlin_mockk 
22
33import  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 
811import  org.junit.jupiter.api.Test 
912
13+ @ExperimentalCoroutinesApi
1014class  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
31136private  suspend  fun  mockSomeDelayedWork (): String  {
32137
33-  delay(10_000 )
138+  delay(5_000 )
34139 return  " Hello World" 
35140
36141}
0 commit comments