Kotlin/kotlinx.coroutines

UncaughtExceptionsBeforeTest: There were uncaught exceptions before the test started.

marlonlom opened this issue · 2 comments

What happened? What should have happened instead?

When running a testcase, it fails and in the stacktrace shows the following trace:

kotlinx.coroutines.test.UncaughtExceptionsBeforeTest: There were uncaught exceptions before the test started. Please avoid this, as such exceptions are also reported in a platform-dependent manner so that they are not lost.
	at app//kotlinx.coroutines.test.TestScopeImpl.enter(TestScope.kt:239)
	at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:309)
	at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
	at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0(TestBuilders.kt:168)
	at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0(Unknown Source)
	at app//kotlinx.coroutines.test.TestBuildersKt__TestBuildersKt.runTest-8Mi8wO0$default(TestBuilders.kt:160)
	at app//kotlinx.coroutines.test.TestBuildersKt.runTest-8Mi8wO0$default(Unknown Source)

Provide a Reproducer
Im using a class name Main

/* MainDispatcherRule: */
@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherRule(
  private val testDispatcher: TestDispatcher = UnconfinedTestDispatcher()
) : TestWatcher() {
  override fun starting(description: Description) {
    Dispatchers.setMain(testDispatcher)
  }

  override fun finished(description: Description) {
    Dispatchers.resetMain()
  }
}
/* test case: */
class SettingsViewModelTest {
@get:Rule
  val mainDispatcherRule = MainDispatcherRule()

  private lateinit var viewModel: SettingsViewModel

  @MockK
  private lateinit var preferencesDataStore: DataStore<Preferences>

  @Before
  fun setup() {
    MockKAnnotations.init(this)
  }

  @Test
  fun `Should return default settings from local storage`() = runTest {
    coEvery { preferencesDataStore.data } returns flowOf(emptyPreferences())
    viewModel = SettingsViewModel(UserPreferencesRepository(preferencesDataStore))
    val uiState = viewModel.uiState.first()
    assertNotNull(uiState)
    when (uiState) {
      is SettingsUiState.Success -> {
        assertNotNull(uiState.settings)
        assertFalse(uiState.settings.useDarkTheme)
        assertTrue(uiState.settings.useDynamicColor)
        assertTrue(uiState.settings.isOnboarding)
      }

      else -> fail()
    }
  }

}

In this scenario, im trying to mock the DataStore, the testcase is configured with mockk library, and, when running the testcase using junit4, it fails woth the stacktrace showing the UncaughtExceptionsBeforeTest Detail.

Please look at the list of the exceptions suppressed by the UncaughtExceptionsBeforeTest and make sure these exceptions aren't thrown. These can also be exceptions that come from other tests but weren't caught in time, because they happened after the execution had already left their runTest block.

This exception happened with me you need to check stubbing data you need to check before initializing the viewmodel you are stubbing if you have something in init block

the error may came from other tests not this test that referenced in the logcat