微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Robolectric 4.4 单元测试错误 - 主循环程序已将未执行的可运行程序排入队列

如何解决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 举报,一经查实,本站将立刻删除。