Kotlin / kotlinx.coroutines

Library support for Kotlin coroutines
Apache License 2.0
12.97k stars 1.84k forks source link

Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used #3454

Closed gbaatsen closed 2 years ago

gbaatsen commented 2 years ago

I saw this issue , but no one was able to provide a good example of the issue. I wanted to provide it, but since the issue is now closed, here's a new one, using coroutines-test:1.6.4. This the repo: https://github.com/gbaatsen/cortest

I have isolated the issue from our main project that I cannot share for obvious reasons. So this project doesn't really do anything, it is just to reproduce the problem. When you run the ConsentDetailViewModelTest we get the message Exception in thread "Test worker @coroutine#3" java.lang.IllegalStateException: Module with the Main dispatcher had failed to initialize. For tests Dispatchers.setMain from kotlinx-coroutines-test module can be used What can be the problem here?

dkhalanskyjb commented 2 years ago

org.junit.Rule is part of JUnit 4, but Test etc are from JUnit 5. So, mainDispatcherRule does not get used at all.

yeeun-yun97 commented 1 year ago

Hi there I am facing MainDispatcher Couroutine in unit test Problem now. Is there any solution to avoid this?

I got solutions from stackoverflow, but Dispatchers.setMain( StandardTestDispatcher()) was not finishing. and Dispatchers.setMain(Dispatchers.Unconfined) throws exception.

your github link repo is returning 404 now, for reference.

4lfg24 commented 1 year ago

I've recreated the issue he is talking about, more or less, here's the repo: https://github.com/4lfg24/stopwatch-app

dkhalanskyjb commented 1 year ago

@4lfg24, the project you linked does not even use the test module.