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。这是一个代码块,但我似乎无法为循环添加 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)
    }
.....
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)