# assertLastValue for RxJava TestObservers

DevFeed: [assertLastValue for RxJava TestObservers](<https://devfeed.tech/articles/assertlastvalue-for-rxjava-testobservers-25323.md>)

Original publisher: [Read original article](<https://kau.sh/blog/rxjava-test-observer-assert-last-value/>)

Author: Kaushik Gopal

Published: 2019-08-18T07:00:00Z

Content type: tutorial

Language: en

Sources: [Kaushik Gopal's Site](<https://devfeed.tech/sources/kaushik-gopal-s-site.md>)

Topics: [RxJava](<https://devfeed.tech/topics/rxjava.md>), [Testing](<https://devfeed.tech/topics/testing.md>), [test](<https://devfeed.tech/topics/test.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [rxjava](<https://devfeed.tech/tags/rxjava.md>), [snippet](<https://devfeed.tech/tags/snippet.md>), [test](<https://devfeed.tech/tags/test.md>), [testing](<https://devfeed.tech/tags/testing.md>)

## AI overview

This tutorial explains how RxJava TestObserver assertions can test emitted stream values, focusing on checking the final value rather than relying on a precise intermediate index. It discusses a checklist example where the final button state is the important result and notes that index-based tests can become brittle when intermediate states change.

## Source excerpt

The TestObserver is an RxJava staple for testing. It allows you to assert values in a stream, in the specific order they were emitted. Here's a quick code snippet from the movies-usf repository 1: @Test fun onSearchingForMovieBladeRunner_shouldSeeSearchResult() { viewModel = MSMainVm(mockApp, mockMovieRepo) val viewStateTester = viewModel.viewState.test() viewModel.processInput(SearchMovieEvent("blade runner 2049")) viewStateTester.assertValueAt(1) { assertThat(it.searchedMovieTitle).isEqualTo("Searching Movie...") true } viewStateTester.assertValueAt(2) { assertThat(it.searchedMovieTitle).isEqualTo("Blade Runner 2049") // ... true } } If you look at the source for the base TestObserver, there are a bunch of these useful methods: assertValue(Predicate valuePredicate): Asserts that this TestObserver/TestSubscriber received exactly one onNext value for which the provided predicate returns true. assertValue(T value): Assert that this TestObserver/TestSubscriber received exactly one onNext value which is equal to the given value with respect to Objects.equals. assertValueAt(int index, Predicate valuePredicate): Asserts that this TestObserver/TestSubscriber received an onNext value at the given index for the provided predicate returns true. assertValueAt(int index, T value) Asserts that this TestObserver/TestSubscriber received an onNext value at the given index which is equal to the given value with respect to null-safe Object.equals. assertValues(T... values) Assert that the TestObserver/TestSubscriber received only the specified values in the specified order. assertValuesOnly(T... values) Assert that the TestObserver/TestSubscriber received only the specified values in the specified order without terminating. While all these operators are useful, most of them require you to know the precise index of the value you're testing. Typically though when i'm testing a feature built with a USF architecture, I really only care about the last value emitted in my test case. When work