junit - Robolectric 4.4 单元测试错误 - 主循环器已将未执行的可运行文件排队
问题描述
与此有关的单元测试错误
Called loadFromPath(/system/framework/framework-res.apk, true); mode=binary sdk=28
java.lang.Exception: Main looper has queued unexecuted runnables.
This might be the cause of the test failure. You might need a shadowOf(getMainLooper()).idle() call.
我们正在使用 Robolectric 4.4 编译到目标 29,但确保在运行单元测试时我们的目标是 28,因为 JDK 仍然是 8 而不是 9。这是一段代码,但我似乎无法在任何地方为 loopers 添加 idle()让这开心
@RunWith(AndroidJUnit4::class)
@LooperMode(LooperMode.Mode.PAUSED)
class MyRoomActivityTest {
@get:Rule
val activityRule = ActivityTestRule(MyRoomActivity::class.java, true, false)
@Inject lateinit var mockViewModel: NewMyRoomActivityViewModel
@Inject lateinit var locationManager: LocationManager
private var testViewStateLiveData: MutableLiveData<NewMyRoomActivityViewModel.MyRoomActivityViewState> = MutableLiveData()
@Before
fun setUp() {
RobolectricTestGEComponent.GraphHolder.testGraph.inject(this)
whenever(mockViewModel.viewState).thenReturn(testViewStateLiveData)
shadowOf(getMainLooper()).idle() // doesn't work here
}
@Test
fun `launch activity sets ViewModel room Id`() {
val roomId = "TestMyRoomId"
shadowOf(getMainLooper()).idle() // doesn't work here either
activityRule.launchActivity(MyRoomActivity.newIntent(ApplicationProvider.getApplicationContext(), roomId)) // fails here all the time
verify(mockViewModel).initialize(roomId)
}
.....
}
解决方案
推荐阅读
- sql-server-agent - 使用 sp_send_dbmail 执行包含链接服务器的存储过程时,SQL Server 代理失败
- python - 带有交叉验证的训练集拆分和测试集拆分的分数
- reactjs - Typescript eslint 显示错误但不显示文件
- python - 我可以链接到不是烧瓶中模板文件夹的html文件吗
- python - 正则表达式在整个列表中分隔标题、名字、姓氏并将数据存储在新列表中
- r - 将值分配给变量名称但作为字符串
- vb.net - 需要 TryParse 的 Visual Basic 程序
- python - 根据其他列值在列值中添加整数
- apache-kafka - 使用带有数百个数据库的 Debezium 时如何管理 Kafka 主题,每个数据库都有数百个表
- python - Wny 是在拟合 Tensorflow 概率模型时的损失,始终为 ```nan```