일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
- FLUTTER
- Kotlin
- UWP
- dart
- Python
- 코틀린
- Android
- Flutter TextField
- C/C++
- kodility
- C
- NDK
- mfc
- Java
- RxAndroid
- Rxjava2
- 알고리즘
- Android P
- RxJava
- 안드로이드
- Django REST Android
- Django REST framework
- C++
- android architecture component
- android push
- 프로그래머스
- 안드로이드 구글맵
- livedata
- Django REST
- flutter firestore
- Today
- Total
개발하는 두더지
Module with the Main dispatcher had failed to initialize. 본문
Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used
JUnit 에서 Coroutine 을 사용하면서 위와 같은 에러메시지가 발생했다.
- 코루틴 테스트 라이브러리 추가
testImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
androidTestImplementation "org.jetbrains.kotlinx:kotlinx-coroutines-test:$coroutinesVersion"
2. 코루틴 Rule 생성
@ExperimentalCoroutinesApi
class MainCoroutineRule : TestWatcher(), TestCoroutineScope by TestCoroutineScope() {
override fun starting(description: Description?) {
super.starting(description)
Dispatchers.setMain(this.coroutineContext[ContinuationInterceptor] as CoroutineDispatcher)
}
override fun finished(description: Description?) {
super.finished(description)
Dispatchers.resetMain()
}
3. 테스트 코드에 Rule 선언
@ExperimentalCoroutinesApi
@get:Rule
var mainCoroutineRule = MainCoroutineRule()
4. 필요한 곳에서 사용
@After
fun releaseViewModel() = mainCoroutineRule.runBlockingTest {
tasksViewModel.deleteAllTasks()
}
@Test
fun loadAllTasksFromRepository_loadingTogglesAndDataLoaded() {
mainCoroutineRule.pauseDispatcher()
tasksViewModel.setFiltering(TasksFilterType.ALL_TASKS)
tasksViewModel.loadTasks(true)
assertThat(LiveDataTestUtil.getValue(tasksViewModel.dataLoading)).isTrue()
mainCoroutineRule.resumeDispatcher()
assertThat(LiveDataTestUtil.getValue(tasksViewModel.dataLoading)).isFalse()
assertThat(LiveDataTestUtil.getValue(tasksViewModel.items)).hasSize(3)
}
'Java,Android' 카테고리의 다른 글
Custom Widget using LinearLayout not getting onDraw() (0) | 2020.04.01 |
---|---|
AndroidX 와 Support 라이브러리 정리 (0) | 2019.12.04 |
Robolectric 사용 중 발생한 에러 해결 (0) | 2019.12.04 |
InAppUpdate 테스트 방법 (0) | 2019.12.04 |
java.lang.RuntimeException: Method getMainLooper in android.os.Looper not mocked. (0) | 2019.10.22 |