Skip to content

Commit c477a63

Browse files
committed
Test: Fix coroutine related failing tests
FYI: This change also introduces 'CoroutineTestRule' to this project.
1 parent 41ea3e1 commit c477a63

File tree

5 files changed

+88
-45
lines changed

5 files changed

+88
-45
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.automattic.simplenote
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlinx.coroutines.ExperimentalCoroutinesApi
5+
import kotlinx.coroutines.test.TestDispatcher
6+
import kotlinx.coroutines.test.resetMain
7+
import kotlinx.coroutines.test.setMain
8+
import org.junit.rules.TestWatcher
9+
import org.junit.runner.Description
10+
11+
@ExperimentalCoroutinesApi
12+
class CoroutineTestRule(val testDispatcher: TestDispatcher) : TestWatcher() {
13+
override fun starting(description: Description) {
14+
super.starting(description)
15+
Dispatchers.setMain(testDispatcher)
16+
}
17+
18+
override fun finished(description: Description) {
19+
super.finished(description)
20+
Dispatchers.resetMain()
21+
}
22+
}

Simplenote/src/test/java/com/automattic/simplenote/repositories/SimperiumCollaboratorsRepositoryTest.kt

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package com.automattic.simplenote.repositories
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+
import com.automattic.simplenote.CoroutineTestRule
45
import com.automattic.simplenote.models.Note
56
import com.simperium.client.Bucket
67
import com.simperium.client.BucketObjectMissingException
78
import kotlinx.coroutines.ExperimentalCoroutinesApi
8-
import kotlinx.coroutines.test.TestCoroutineDispatcher
9-
import kotlinx.coroutines.test.runBlockingTest
9+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
10+
import kotlinx.coroutines.test.runTest
1011
import org.junit.Assert.*
1112
import org.junit.Before
1213
import org.junit.Rule
@@ -19,11 +20,16 @@ import org.mockito.kotlin.whenever
1920
class SimperiumCollaboratorsRepositoryTest {
2021
@get:Rule
2122
val rule = InstantTaskExecutorRule()
23+
@get:Rule
24+
val coroutinesTestRule = CoroutineTestRule(UnconfinedTestDispatcher())
2225

2326
private val mockBucket: Bucket<*> = mock(Bucket::class.java)
2427
private val notesBucket = mock(Bucket::class.java) as Bucket<Note>
2528

26-
private val collaboratorsRepository = SimperiumCollaboratorsRepository(notesBucket, TestCoroutineDispatcher())
29+
private val collaboratorsRepository = SimperiumCollaboratorsRepository(
30+
notesBucket,
31+
coroutinesTestRule.testDispatcher
32+
)
2733

2834
private val noteId = "key1"
2935
private val note = Note(noteId).apply {
@@ -66,15 +72,15 @@ class SimperiumCollaboratorsRepositoryTest {
6672
}
6773

6874
@Test
69-
fun getCollaboratorsShouldReturnJustEmails() = runBlockingTest {
75+
fun getCollaboratorsShouldReturnJustEmails() = runTest {
7076
val expected = CollaboratorsActionResult.CollaboratorsList(listOf("[email protected]", "[email protected]"))
7177
val result = collaboratorsRepository.getCollaborators(noteId)
7278

7379
assertEquals(expected, result)
7480
}
7581

7682
@Test
77-
fun getCollaboratorsWhenNoteInTrashShouldReturnError() = runBlockingTest {
83+
fun getCollaboratorsWhenNoteInTrashShouldReturnError() = runTest {
7884
note.isDeleted = true
7985

8086
val result = collaboratorsRepository.getCollaborators(noteId)
@@ -84,7 +90,7 @@ class SimperiumCollaboratorsRepositoryTest {
8490
}
8591

8692
@Test
87-
fun getCollaboratorsWhenNoteIsDeletedShouldReturnError() = runBlockingTest {
93+
fun getCollaboratorsWhenNoteIsDeletedShouldReturnError() = runTest {
8894
whenever(notesBucket.get(any())).thenThrow(BucketObjectMissingException())
8995

9096
val result = collaboratorsRepository.getCollaborators(noteId)
@@ -94,7 +100,7 @@ class SimperiumCollaboratorsRepositoryTest {
94100
}
95101

96102
@Test
97-
fun addCollaboratorShouldAddATagToNote() = runBlockingTest {
103+
fun addCollaboratorShouldAddATagToNote() = runTest {
98104
val collaborator = "[email protected]"
99105

100106
val result = collaboratorsRepository.addCollaborator(noteId, collaborator)
@@ -105,7 +111,7 @@ class SimperiumCollaboratorsRepositoryTest {
105111
}
106112

107113
@Test
108-
fun addCollaboratorWhenNoteInTrashShouldReturnError() = runBlockingTest {
114+
fun addCollaboratorWhenNoteInTrashShouldReturnError() = runTest {
109115
note.isDeleted = true
110116
val collaborator = "[email protected]"
111117

@@ -116,7 +122,7 @@ class SimperiumCollaboratorsRepositoryTest {
116122
}
117123

118124
@Test
119-
fun addCollaboratorWhenNoteIsDeletedShouldReturnError() = runBlockingTest {
125+
fun addCollaboratorWhenNoteIsDeletedShouldReturnError() = runTest {
120126
whenever(notesBucket.get(any())).thenThrow(BucketObjectMissingException())
121127
val collaborator = "[email protected]"
122128

@@ -127,7 +133,7 @@ class SimperiumCollaboratorsRepositoryTest {
127133
}
128134

129135
@Test
130-
fun removeCollaboratorShouldAddATagToNote() = runBlockingTest {
136+
fun removeCollaboratorShouldAddATagToNote() = runTest {
131137
val collaborator = "[email protected]"
132138

133139
val result = collaboratorsRepository.removeCollaborator(noteId, collaborator)
@@ -138,7 +144,7 @@ class SimperiumCollaboratorsRepositoryTest {
138144
}
139145

140146
@Test
141-
fun removeCollaboratorWhenNoteInTrashShouldReturnError() = runBlockingTest {
147+
fun removeCollaboratorWhenNoteInTrashShouldReturnError() = runTest {
142148
note.isDeleted = true
143149
val collaborator = "[email protected]"
144150

@@ -149,7 +155,7 @@ class SimperiumCollaboratorsRepositoryTest {
149155
}
150156

151157
@Test
152-
fun removeCollaboratorWhenNoteIsDeletedShouldReturnError() = runBlockingTest {
158+
fun removeCollaboratorWhenNoteIsDeletedShouldReturnError() = runTest {
153159
whenever(notesBucket.get(any())).thenThrow(BucketObjectMissingException())
154160
val collaborator = "[email protected]"
155161

Simplenote/src/test/java/com/automattic/simplenote/viewmodels/AddCollaboratorViewModelTest.kt

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.automattic.simplenote.viewmodels
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+
import com.automattic.simplenote.CoroutineTestRule
45
import com.automattic.simplenote.authentication.SessionManager
56
import com.automattic.simplenote.models.Note
67
import com.automattic.simplenote.repositories.SimperiumCollaboratorsRepository
@@ -9,8 +10,8 @@ import com.simperium.client.Bucket
910
import com.simperium.client.BucketObjectMissingException
1011
import com.simperium.client.User
1112
import kotlinx.coroutines.ExperimentalCoroutinesApi
12-
import kotlinx.coroutines.test.TestCoroutineDispatcher
13-
import kotlinx.coroutines.test.runBlockingTest
13+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
14+
import kotlinx.coroutines.test.runTest
1415
import org.junit.Assert.assertEquals
1516
import org.junit.Before
1617
import org.junit.Rule
@@ -23,9 +24,14 @@ import org.mockito.kotlin.whenever
2324
class AddCollaboratorViewModelTest {
2425
@get:Rule
2526
val rule = InstantTaskExecutorRule()
27+
@get:Rule
28+
val coroutinesTestRule = CoroutineTestRule(UnconfinedTestDispatcher())
2629

2730
private val notesBucket = Mockito.mock(Bucket::class.java) as Bucket<Note>
28-
private val collaboratorsRepository = SimperiumCollaboratorsRepository(notesBucket, TestCoroutineDispatcher())
31+
private val collaboratorsRepository = SimperiumCollaboratorsRepository(
32+
notesBucket,
33+
coroutinesTestRule.testDispatcher
34+
)
2935
private val simperium = Mockito.mock(Simperium::class.java)
3036
private val viewModel = AddCollaboratorViewModel(collaboratorsRepository, SessionManager(simperium))
3137

@@ -47,21 +53,21 @@ class AddCollaboratorViewModelTest {
4753
}
4854

4955
@Test
50-
fun addValidCollaboratorShouldTriggerAddedEvent() = runBlockingTest {
56+
fun addValidCollaboratorShouldTriggerAddedEvent() = runTest {
5157
viewModel.addCollaborator(noteId,"[email protected]")
5258

5359
assertEquals(AddCollaboratorViewModel.Event.CollaboratorAdded, viewModel.event.value)
5460
}
5561

5662
@Test
57-
fun addInvalidCollaboratorShouldTriggerInvalidEvent() = runBlockingTest {
63+
fun addInvalidCollaboratorShouldTriggerInvalidEvent() = runTest {
5864
viewModel.addCollaborator(noteId, "test@emil")
5965

6066
assertEquals(AddCollaboratorViewModel.Event.InvalidCollaborator, viewModel.event.value)
6167
}
6268

6369
@Test
64-
fun addValidCollaboratorToNoteInTrashShouldTriggerNoteInTrash() = runBlockingTest {
70+
fun addValidCollaboratorToNoteInTrashShouldTriggerNoteInTrash() = runTest {
6571
note.isDeleted = true
6672

6773
viewModel.addCollaborator(noteId, "[email protected]")
@@ -70,7 +76,7 @@ class AddCollaboratorViewModelTest {
7076
}
7177

7278
@Test
73-
fun addValidCollaboratorToNoteDeletedShouldTriggerNoteDeleted() = runBlockingTest {
79+
fun addValidCollaboratorToNoteDeletedShouldTriggerNoteDeleted() = runTest {
7480
whenever(notesBucket.get(any())).thenThrow(BucketObjectMissingException())
7581

7682
viewModel.addCollaborator(noteId, "[email protected]")
@@ -79,7 +85,7 @@ class AddCollaboratorViewModelTest {
7985
}
8086

8187
@Test
82-
fun addSameAccountShouldTriggerCollaboratorCurrentUser() = runBlockingTest {
88+
fun addSameAccountShouldTriggerCollaboratorCurrentUser() = runTest {
8389
viewModel.addCollaborator(noteId, "[email protected]")
8490

8591
assertEquals(AddCollaboratorViewModel.Event.CollaboratorCurrentUser, viewModel.event.value)

Simplenote/src/test/java/com/automattic/simplenote/viewmodels/CollaboratorsViewModelTest.kt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package com.automattic.simplenote.viewmodels
22

33
import androidx.arch.core.executor.testing.InstantTaskExecutorRule
4+
import com.automattic.simplenote.CoroutineTestRule
45
import com.automattic.simplenote.repositories.CollaboratorsActionResult
56
import com.automattic.simplenote.repositories.CollaboratorsRepository
67
import com.automattic.simplenote.viewmodels.CollaboratorsViewModel.Event
78
import com.automattic.simplenote.viewmodels.CollaboratorsViewModel.UiState
89
import kotlinx.coroutines.ExperimentalCoroutinesApi
910
import kotlinx.coroutines.flow.flow
10-
import kotlinx.coroutines.test.runBlockingTest
11+
import kotlinx.coroutines.test.UnconfinedTestDispatcher
12+
import kotlinx.coroutines.test.runTest
1113
import org.junit.Assert.assertEquals
1214
import org.junit.Before
1315
import org.junit.Rule
@@ -19,29 +21,30 @@ import org.mockito.kotlin.whenever
1921
class CollaboratorsViewModelTest {
2022
@get:Rule
2123
val rule = InstantTaskExecutorRule()
24+
@get:Rule
25+
val coroutinesTestRule = CoroutineTestRule(UnconfinedTestDispatcher())
2226

2327
private val mockCollaboratorsRepository = mock(CollaboratorsRepository::class.java)
2428
private val viewModel = CollaboratorsViewModel(mockCollaboratorsRepository)
2529

2630
private val noteId = "key1"
2731

28-
2932
@Before
30-
fun setup() = runBlockingTest {
33+
fun setup() = runTest {
3134
whenever(mockCollaboratorsRepository.getCollaborators(noteId))
3235
.thenReturn(CollaboratorsActionResult.CollaboratorsList(listOf("[email protected]", "[email protected]")))
3336
}
3437

3538
@Test
36-
fun loadCollaboratorsShouldUpdateUiStateWithList() = runBlockingTest {
39+
fun loadCollaboratorsShouldUpdateUiStateWithList() = runTest {
3740
viewModel.loadCollaborators(noteId)
3841

3942
val expectedCollaborators = UiState.CollaboratorsList(listOf("[email protected]", "[email protected]"))
4043
assertEquals(expectedCollaborators, viewModel.uiState.value)
4144
}
4245

4346
@Test
44-
fun loadEmptyCollaboratorsShouldUpdateUiStateWithEmpty() = runBlockingTest {
47+
fun loadEmptyCollaboratorsShouldUpdateUiStateWithEmpty() = runTest {
4548
whenever(mockCollaboratorsRepository.getCollaborators(noteId))
4649
.thenReturn(CollaboratorsActionResult.CollaboratorsList(emptyList()))
4750

@@ -51,7 +54,7 @@ class CollaboratorsViewModelTest {
5154
}
5255

5356
@Test
54-
fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteInTrash() = runBlockingTest {
57+
fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteInTrash() = runTest {
5558
whenever(mockCollaboratorsRepository.getCollaborators(noteId))
5659
.thenReturn(CollaboratorsActionResult.NoteInTrash)
5760

@@ -61,7 +64,7 @@ class CollaboratorsViewModelTest {
6164
}
6265

6366
@Test
64-
fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteDeleted() = runBlockingTest {
67+
fun loadCollaboratorsForNoteInTrashShouldUpdateUiStateNoteDeleted() = runTest {
6568
whenever(mockCollaboratorsRepository.getCollaborators(noteId))
6669
.thenReturn(CollaboratorsActionResult.NoteDeleted)
6770

@@ -71,7 +74,7 @@ class CollaboratorsViewModelTest {
7174
}
7275

7376
@Test
74-
fun removeCollaboratorShouldReturnListEmails() = runBlockingTest {
77+
fun removeCollaboratorShouldReturnListEmails() = runTest {
7578
viewModel.loadCollaborators(noteId)
7679
whenever(mockCollaboratorsRepository.removeCollaborator(noteId, "[email protected]"))
7780
.thenReturn(CollaboratorsActionResult.CollaboratorsList(listOf("[email protected]")))
@@ -83,7 +86,7 @@ class CollaboratorsViewModelTest {
8386
}
8487

8588
@Test
86-
fun removeLastCollaboratorShouldReturnEmpty() = runBlockingTest {
89+
fun removeLastCollaboratorShouldReturnEmpty() = runTest {
8790
viewModel.loadCollaborators(noteId)
8891
whenever(mockCollaboratorsRepository.removeCollaborator(noteId, "[email protected]"))
8992
.thenReturn(CollaboratorsActionResult.CollaboratorsList(emptyList()))
@@ -94,7 +97,7 @@ class CollaboratorsViewModelTest {
9497
}
9598

9699
@Test
97-
fun removeCollaboratorForNoteInTrashShouldTriggerEvent() = runBlockingTest {
100+
fun removeCollaboratorForNoteInTrashShouldTriggerEvent() = runTest {
98101
viewModel.loadCollaborators(noteId)
99102
whenever(mockCollaboratorsRepository.removeCollaborator(noteId, "[email protected]"))
100103
.thenReturn(CollaboratorsActionResult.NoteInTrash)
@@ -105,7 +108,7 @@ class CollaboratorsViewModelTest {
105108
}
106109

107110
@Test
108-
fun removeCollaboratorForNoteDeletedShouldTriggerEvent() = runBlockingTest {
111+
fun removeCollaboratorForNoteDeletedShouldTriggerEvent() = runTest {
109112
viewModel.loadCollaborators(noteId)
110113
whenever(mockCollaboratorsRepository.removeCollaborator(noteId, "[email protected]"))
111114
.thenReturn(CollaboratorsActionResult.NoteDeleted)
@@ -140,7 +143,7 @@ class CollaboratorsViewModelTest {
140143
}
141144

142145
@Test
143-
fun collaboratorAddedAfterStoppedListeningChangesShouldNotUpdateUiState() = runBlockingTest {
146+
fun collaboratorAddedAfterStoppedListeningChangesShouldNotUpdateUiState() = runTest {
144147
viewModel.loadCollaborators(noteId)
145148
viewModel.startListeningChanges()
146149
viewModel.stopListeningChanges()
@@ -154,7 +157,7 @@ class CollaboratorsViewModelTest {
154157
}
155158

156159
@Test
157-
fun collaboratorAddedShouldUpdateUiState() = runBlockingTest {
160+
fun collaboratorAddedShouldUpdateUiState() = runTest {
158161
viewModel.loadCollaborators(noteId)
159162
whenever(mockCollaboratorsRepository.collaboratorsChanged(noteId)).thenReturn(flow { emit(true) })
160163
val expectedList = listOf("[email protected]", "[email protected]", "[email protected]")

0 commit comments

Comments
 (0)