Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UncaughtExceptionsBeforeTest: There were uncaught exceptions before the test started. #4070

Closed
marlonlom opened this issue Mar 17, 2024 · 2 comments

Comments

@marlonlom
Copy link

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.

@dkhalanskyjb
Copy link
Collaborator

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.

@dkhalanskyjb dkhalanskyjb closed this as not planned Won't fix, can't repro, duplicate, stale Mar 18, 2024
@MahmoudLkashef
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants