Lifecycle-Aware Components in Android

Nov 1 2022 Kotlin 1.6, Android 12, Android Studio Chipmunk 2021.2.1

Part 1: Lifecycle-Aware Components in Android

12. Test a Lifecycle-Aware Component

Episode complete

Save for later
About this episode
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Use LiveData With a Fragment

This video Test a Lifecycle-Aware Component was last updated on Nov 1 2022

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

You can unlock the rest of this video course, and our entire catalogue of books and videos, with a kodeco.com Professional subscription.

Congratulations, for reaching so far in this course. At the moment, I am sure - you know a lot about lifecycle-aware components.

  private val lifecycleOwner = mockk<LifecycleOwner>(relaxed = true)
  private val networkMonitor = mockk<NetworkMonitor>(relaxed = true)
  private lateinit var lifecycleRegistry: LifecycleRegistry
    lifecycleRegistry = LifecycleRegistry(lifecycleOwner)
    lifecycleRegistry.addObserver(networkMonitor)
  @Before
  fun setup() {
    lifecycleRegistry = LifecycleRegistry(lifecycleOwner)
    lifecycleRegistry.addObserver(networkMonitor)
  }
  @Test
  fun `When dispatching ON_CREATE lifecycle event, call onCreate()`() {

  }
    verify {
      networkMonitor.onCreate(lifecycleOwner)
    }
  @Test
  fun `When dispatching ON_CREATE lifecycle event, call onCreate()`() {
    // 1. Notify observers and set the lifecycle state.
    lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_CREATE)

    // 2. Verify the execution of the correct method.
    verify {
      networkMonitor.onCreate(lifecycleOwner)
    }
  }
  @Test
  fun `When dispatching ON_START lifecycle event, call onStart()`() {
    // 1. Notify observers and set the lifecycle state.
    lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)

    verify {
      networkMonitor.onStart(lifecycleOwner)
    }
  }
  @Test
  fun `When dispatching ON_STOP lifecycle event, call onStop()`() {
    // 1. Notify observers and set the lifecycle state.
    lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_START)
    lifecycleRegistry.handleLifecycleEvent(Lifecycle.Event.ON_STOP)

    // 2. Verify the execution of the correct method.
    verify {
      networkMonitor.onStop(lifecycleOwner)
    }
  }