Skip to content

Instantly share code, notes, and snippets.

@Abdelsattar
Created March 21, 2021 15:35
Show Gist options
  • Select an option

  • Save Abdelsattar/aeb2630df893febebd135b6cc47cc6a9 to your computer and use it in GitHub Desktop.

Select an option

Save Abdelsattar/aeb2630df893febebd135b6cc47cc6a9 to your computer and use it in GitHub Desktop.
Fix problem live data with Junit 5
import androidx.arch.core.executor.ArchTaskExecutor
import androidx.arch.core.executor.TaskExecutor
import org.junit.jupiter.api.extension.AfterEachCallback
import org.junit.jupiter.api.extension.BeforeEachCallback
import org.junit.jupiter.api.extension.ExtensionContext
class InstantExecutorExtension : BeforeEachCallback, AfterEachCallback {
override fun beforeEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance().setDelegate(object : TaskExecutor() {
override fun executeOnDiskIO(runnable: Runnable) = runnable.run()
override fun postToMainThread(runnable: Runnable) = runnable.run()
override fun isMainThread(): Boolean = true
})
}
override fun afterEach(context: ExtensionContext?) {
ArchTaskExecutor.getInstance().setDelegate(null)
}
}
// based on this answer https://stackoverflow.com/a/57843898/3553843
// and then add like this
@ExtendWith(InstantExecutorExtension::class)
class ContentViewModelTest {
}
that works for me
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment