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

About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Use LiveData With a Fragment

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

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

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)
    }
  }