0

I have a FileRepository class to fill MutableLiveData. I have two functions in there. One is calling web service and setting list (getAllFilms). Other one is assigning MutableLiveData with first function List (getFilmData). I try to write unit test for MutableLiveData. Can you help me? This function data always comes null.

public class FilmRepositoryTest

@Mock
FilmRepository frepo;

@Rule
public InstantTaskExecutorRule instantTaskExecutorRule = new InstantTaskExecutorRule();


@Before
public void setUp() throws Exception {
}

@Test
public void getFilmData_forData() throws IOException {
    ArrayList<film> filmlistesi = new ArrayList<>();
    MutableLiveData<List<film>> bilgi = new MutableLiveData<>();
    String arama = "ankara";
    filmlistesi.add(new film("Behzat Ç.: Bir Ankara Polisiyesi","2010–2019","tt1795096","series","https://m.media-amazon.com/images/M/MV5BZDZjY2I5ZjEtZGE2MS00ZjRmLTlmMGEtMDQ5ZmZhZWJjYzk3XkEyXkFqcGdeQXVyNDg4MjkzNDk@._V1_SX300.jpg"));

    when(frepo.getAllFilms(arama)).thenReturn(filmlistesi);
    bilgi.setValue(filmlistesi);
    System.out.println(frepo.getFilmData(arama)); // print(NULL)
    System.out.println(bilgi.getValue());         // print(filmlistesi)
    assertEquals(frepo.getFilmData(arama),bilgi.getValue());

}

public class FilmRepository

public MutableLiveData<List<film>> getFilmData(String a) throws IOException {
    MutableLiveData<List<film>> data = new MutableLiveData<>();
    data.setValue(getAllFilms(a));
    return data;
}

public ArrayList getAllFilms(String filmName) throws IOException {
    ArrayList<film> dataset = new ArrayList<>();
    return dataset;
}
Kamil
  • 175
  • 1
  • 1
  • 10
  • 2
    There is no relation with inner mocked methods. Look at [this link](https://stackoverflow.com/questions/34205105/set-value-to-mocked-object-but-get-null) – dralexnumber Sep 18 '19 at 09:17

1 Answers1

0

LiveData or MutableLiveData must have an Observer set of it or else it returns null

Observer<List<film>> observer = new Observer<List<film>>() {

        @Override
        public void onChanged(@Nullable List<film> films) {
            assertEquals(frepo.getFilmData(arama), films.getValue());
            biligi.removeObserver(this)
        }

};

bilgi.observeForever(observer);
bilgi.setValue(filmlistesi);
Kirill Matrosov
  • 5,564
  • 4
  • 28
  • 39
  • Yeah, I have an observer in MainActivity class. When I write unit test, it comes null. Normally, It is working. Another question is that where can i use getAllFilms function. Your code is not use this but getFilmData getting List from the getAllFilms function – Kamil Sep 18 '19 at 08:45
  • you've set @Rule as InstantTaskExecutorRule and not ActivtiyTestRule, because of this while you run the test MainActivity doesn't run, so it doesn't matter wether you've set an observer in MainActivity or not. Second about getAllFilms function, you are not doing anything with the parameter passed and simply returning an empty ArrayList. Where to call the method depends on you implementation logic – Shashank Gaurav Sep 18 '19 at 09:00
  • I cut my code because of the confusing. getAllFilms function -> get data from api and write realm database. Then, return List. I need to use that for testing – Kamil Sep 18 '19 at 09:08
  • if you are fetching data from the API then that cannot be tested with InstantTestExecutorRule, you've to use ActivityTestRule and run it on the device, also you've to add an interface/callback to in your Repository to let you know when the data has been fetched from the API – Shashank Gaurav Sep 18 '19 at 10:01