espresso 测试时房间数据库错误 "Cannot access database on the main thread"
Room Database Error when espresso test "Cannot access database on the main thread"
我正在使用浓缩咖啡进行测试,但在执行以下方法时出现错误。
viewModel.getData().observe(viewLifecycleOwner, Observer { options ->
Log.d(TAG, "onViewCreated: $options")
val data = options.filter { option -> option.type!! == OptionType.DATA}
updateData.updateUIComponent(data)
})
获取数据方法return 一个 LiveData 对象。这在 Fragments 中运行良好,但在 espresso 测试中不起作用 class
Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
这是我的测试class
@LargeTest
@RunWith(AndroidJUnit4::class)
open class BaseIntegrationTest {
@get:Rule
val instantTestExecutorRule = InstantTaskExecutorRule()
lateinit var navController: TestNavHostController
lateinit var fragmentScenario: FragmentScenario<DetailsFragment>
@Before
fun initTest() {
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
}
@Test
fun launchFragment() {
val bundle = Bundle()
fragmentScenario =
launchFragmentInContainer(bundle, themeResId = R.style.Theme_mainTheme)
fragmentScenario.moveToState(Lifecycle.State.STARTED)
UiThreadStatement.runOnUiThread {
navController.setGraph(R.navigation.app_navigation_graph)
}
fragmentScenario.onFragment { fragment ->
//CollectFeedbackFragment()
Navigation.setViewNavController(fragment.requireView(), navController)
}
onView(withId(R.id.progressBar)).isVisible()
}
}
我设法通过使用 Dispatchers.IO 和 Dispatchers.Main
解决了这个问题
class SampleViewModel : ViewModel() {
fun getData():DataLiveData {
viewModelScope.launch {
withContext(Dispatchers.IO) {
// here I am seding the Room db call
withContext(Dispatchers.Main) {
// I am updating the MutableLiveData object here
}
}
}
return mutableLiveData
}
}
并且不要忘记注入调度程序而不是硬编码。
点击 here!
我正在使用浓缩咖啡进行测试,但在执行以下方法时出现错误。
viewModel.getData().observe(viewLifecycleOwner, Observer { options ->
Log.d(TAG, "onViewCreated: $options")
val data = options.filter { option -> option.type!! == OptionType.DATA}
updateData.updateUIComponent(data)
})
获取数据方法return 一个 LiveData 对象。这在 Fragments 中运行良好,但在 espresso 测试中不起作用 class
Cannot access database on the main thread since it may potentially lock the UI for a long period of time.
这是我的测试class
@LargeTest
@RunWith(AndroidJUnit4::class)
open class BaseIntegrationTest {
@get:Rule
val instantTestExecutorRule = InstantTaskExecutorRule()
lateinit var navController: TestNavHostController
lateinit var fragmentScenario: FragmentScenario<DetailsFragment>
@Before
fun initTest() {
navController = TestNavHostController(
ApplicationProvider.getApplicationContext()
)
}
@Test
fun launchFragment() {
val bundle = Bundle()
fragmentScenario =
launchFragmentInContainer(bundle, themeResId = R.style.Theme_mainTheme)
fragmentScenario.moveToState(Lifecycle.State.STARTED)
UiThreadStatement.runOnUiThread {
navController.setGraph(R.navigation.app_navigation_graph)
}
fragmentScenario.onFragment { fragment ->
//CollectFeedbackFragment()
Navigation.setViewNavController(fragment.requireView(), navController)
}
onView(withId(R.id.progressBar)).isVisible()
}
}
我设法通过使用 Dispatchers.IO 和 Dispatchers.Main
解决了这个问题class SampleViewModel : ViewModel() {
fun getData():DataLiveData {
viewModelScope.launch {
withContext(Dispatchers.IO) {
// here I am seding the Room db call
withContext(Dispatchers.Main) {
// I am updating the MutableLiveData object here
}
}
}
return mutableLiveData
}
}
并且不要忘记注入调度程序而不是硬编码。 点击 here!