简体   繁体   English

如何测试 android 架构组件生命周期事件?

[英]How can I test for android architecture components lifecycle events?

I have abstract class where i can register lifecycle event:我有抽象类,我可以在其中注册生命周期事件:

abstract class PollingServiceAbs<T> : LifecycleObserver {

    private var timer: Timer? = null

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    open fun start() {
        try {
            timer = Timer()
            timer?.schedule(object : TimerTask() {
                override fun run() {
                    forcedFetch()
                }
            }, 0, POLLING_TIMEOUT)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    open fun stop() {
        try {
            timer?.cancel()
            timer?.purge()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        timer = null
    }


    fun register(lifecycle: Lifecycle) {
        lifecycle.addObserver(this)
    }

And in my presenter i have next fun where i register observer:在我的演示者中,我注册观察者的下一个乐趣是:

fun onCreate(lifecycle: Lifecycle) {
    pollingService.contract = this
    pollingService.register(lifecycle)
}

How can I test my onStart() and onStop() functions ?如何测试我的onStart()onStop()函数?

UPD: Write next implementation, but markstate is deprecated: UPD:编写下一个实现,但不推荐使用 markstate:

@Test
@Throws(Exception::class)
fun onCreateTest() {
    val lifecycleOwner: LifecycleOwner = mock(LifecycleOwner::class.java)
    val lifecycle = LifecycleRegistry(mock(LifecycleOwner::class.java))
    lifecycle.markState(Lifecycle.State.RESUMED)

    Mockito.`when`(lifecycleOwner.lifecycle).thenReturn(lifecycle)

    createPresenter(viewContract = mockView).onCreate(lifecycle)

    assertNotNull(mockView)
    Mockito.verify(mockPollingService).start()
    Mockito.verifyNoMoreInteractions(mockView)
}

You can use setCurrentState instead of deprecated markState .您可以使用setCurrentState而不是已弃用的markState Or you can handleLifecycleEvent with appropriate event或者您可以使用适当的事件处理handleLifecycleEvent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android架构组件生命周期状态 - Android Architecture Components Lifecycle state 如何为 android 架构组件生命周期事件添加单元测试? - How can I add unit test for android architecture components life cycle event? Android架构组件GithubBrowserSample单元测试理解 - Android Architecture Components GithubBrowserSample unit test understanding 如何使用Architecture Components观察数据库删除事件 - How to observe database delete events with Architecture Components 如何利用Android AdMob生命周期事件 - How to make use of Android AdMob lifecycle events Android MVP和Lifecycle体系结构组件 - Android MVP and Lifecycle architecture component Android架构组件LiveData - 如何将broadcastReceivers绑定到生命周期 - Android Architecture component LiveData - how to bind broadcastReceivers to lifecycle 如何测试应用程序单例和Android活动生命周期? - How to test Application singletons and the Android activity lifecycle? 如何在仪器测试 android 中获得生命周期所有者? - How to get lifecycle owner in instrumented test android? 我如何通过 textview 在屏幕上显示发生在 android 中的生命周期事件 - How do i display the lifecycle events occuring in android on screen through textview
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM