How to get non null result from getValue() of mapped LiveData without calling observe()?

Viewed 1714

I'm using Transformations.map method to get a new LiveData based on original one. While getValue method of the original returns always a correct value, the same accessor of the mapped on returns null.

How can I solve or workaround this issue in order to test the class exposing LiveData without calling observe on it?

Here is the code explaining this problem:

public class LiveDataTest {

    @Rule
    public TestRule rule = new InstantTaskExecutorRule();

    @Test
    public void mapTest() {
        final MutableLiveData<String> original = new MutableLiveData<>();
        final LiveData<String> mapped = Transformations.map(original, input -> "Mapped: " + input);

        System.out.println(original.getValue()); // null - OK
        System.out.println(mapped.getValue()); // null - OK

        original.setValue("Hello, World!");

        System.out.println(original.getValue());  // "Hello, World!" - OK
        System.out.println(mapped.getValue()); // null - Should be "Mapped: Hello, World!"
    }
}
1 Answers
Related