Skip to content
TestObserver to easily test LiveData and make assertions on them.
Java Kotlin
Branch: master
Clone or download

LiveData Testing

TestObserver to easily test LiveData and make assertions on them.

CircleCI Download License Android Arsenal

Read Medium Article for more info.

Explanatory Diagram


Having LiveData<Integer> of counter from 0 to 4:

Kotlin - see ExampleTest.kt

  .assertValue { it > 3 }
  .assertNever { it > 4 }

// Assertion on structures with a lot of nesting { it.items[0].header.title }
  .assertValue("Expected title")

Java - see

  .assertValue(value -> value > 3)
  .assertNever(value -> value > 4);

Don't forget to use InstantTaskExecutorRule from androidx.arch.core:core-testing to make your LiveData test run properly.


Kotlin users:
testImplementation 'com.jraska.livedata:testing-ktx:1.1.1'
Java users:
testImplementation 'com.jraska.livedata:testing:1.1.1'

If you are not using androidx namespace yet, please use version 0.2.1. Explanation here.


This library is created in a belief that to effective and valuable test should be fast to write and model real code usage. As by Architecture components spec Activity should communicate with its ViewModel only through observing LiveData. TestObserver in this case simulates the Activity and by testing LiveData, we could test our whole logic except the View where the responsibility belongs to Activity. Key ideas:

  • Test pretends to be an Activity
  • No Android framework mocking or Robolectric - just standard fast JUnit tests
  • Fluent API inspired by RxJava TestObserver
  • Easy to write fast executing tests - possibly TDD
You can’t perform that action at this time.