Как правильно проверить проверку viewModel.getMoviesByReleaseDate()
получает List<DiscoverMovie>
при вызове repository.getMoviesByReleaseDate()
на viewModel.init()
.
Мой метод testRepoFetchDataSuccess()
не достигает вышеупомянутого. Пожалуйста, приведите пример кода, если это возможно, поскольку я новичок в модульном тестировании.
public class TMDbRepository {
private static final String TAG = "TMDbRepository";
private static final String RELEASE_DATE_LTE = "2020-02-02";
private static final String RELEASE_DATE_DESC = "release_date.desc";
private static final int PAGES_NO = 1;
private final TMDbWebService webService;
@Inject
public TMDbRepository(TMDbWebService webService) {
this.webService = webService;
}
public Single<DiscoverMovieResponse> getMoviesByReleaseDate() {
return webService
.getMoviesByReleaseDate(RELEASE_DATE_LTE, RELEASE_DATE_DESC, PAGES_NO);
}
public Single<MovieDetails> getMovieDetails(int id) {
return webService.getMovieDetails(id);
}
}
public class MasterFragmentViewModel extends ViewModel {
private static final String TAG = "MasterFragmentViewModel";
private final RxSingleSchedulers rxSingleSchedulers;
private final TMDbRepository repository;
private CompositeDisposable disposable;
private final MutableLiveData<List<DiscoverMovie>> movies = new MutableLiveData<>();
private final MutableLiveData<Boolean> error = new MutableLiveData<>();
private final MutableLiveData<Boolean> loading = new MutableLiveData<>();
@Inject
public MasterFragmentViewModel(TMDbRepository repository, RxSingleSchedulers rxSingleSchedulers) {
this.repository = repository;
this.rxSingleSchedulers = rxSingleSchedulers;
disposable = new CompositeDisposable();
}
public void init() {
fetchMoviesByReleaseDate();
}
private void fetchMoviesByReleaseDate() {
loading.setValue(true);
disposable.add(
repository.getMoviesByReleaseDate()
.compose(rxSingleSchedulers.applySchedulers())
.subscribeWith(new DisposableSingleObserver<DiscoverMovieResponse>() {
@Override
public void onSuccess(DiscoverMovieResponse discoverMovieResponse) {
error.setValue(false);
movies.setValue(discoverMovieResponse.getMovieList());
loading.setValue(false);
// Log.d(TAG, "onSuccess: " + discoverMovieResponse.getTotalResults());
}
@Override
public void onError(Throwable e) {
Log.d(TAG, "onError: " + e);
error.setValue(true);
loading.setValue(false);
}
})
);
}
public LiveData<List<DiscoverMovie>> getMoviesByReleaseDate() {
return movies;
}
public LiveData<Boolean> getError() {
return error;
}
public LiveData<Boolean> getLoading() {
return loading;
}
@Override
protected void onCleared() {
super.onCleared();
disposable.clear();
Log.d(TAG, "onCleared: disposable cleared!");
}
}
public class MasterFragmentTest {
@Rule
public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();
@Mock
TMDbRepository repository;
@Mock
LifecycleOwner lifecycleOwner;
@Mock
Observer<List<DiscoverMovie>> moviesObserver;
Lifecycle lifecycle;
private MasterFragmentViewModel viewModel;
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
lifecycle = new LifecycleRegistry(lifecycleOwner);
viewModel = new MasterFragmentViewModel(repository, RxSingleSchedulers.TEST_SCHEDULER);
viewModel.getMoviesByReleaseDate().observeForever(moviesObserver);
}
@Test
public void testNull() {
when(repository.getMoviesByReleaseDate()).thenReturn(null);
assertNotNull(viewModel.getMoviesByReleaseDate());
assertTrue(viewModel.getMoviesByReleaseDate().hasObservers());
}
@Test
public void testRepoFetchDataSuccess() {
List<DiscoverMovie> mockList = new ArrayList<>();
DiscoverMovie discoverMovie = Mockito.mock(DiscoverMovie.class);
mockList.add(discoverMovie);
when(repository.getMoviesByReleaseDate()).thenReturn(Single.just(new DiscoverMovieResponse()));
viewModel.init();
verify(moviesObserver).onChanged(mockList);
}
@After
public void tearDown() throws Exception {
repository = null;
viewModel = null;
}
}
Результат теста:
Argument(s) are different! Wanted:
moviesObserver.onChanged(
[Mock for DiscoverMovie, hashCode: 990897274]
);
-> at com.sulaz.tmdb.MasterViewModelTest.testRepoFetchDataSuccess(MasterViewModelTest.java:72)
Actual invocation has different arguments:
moviesObserver.onChanged(
null
);
-> at androidx.lifecycle.LiveData.considerNotify(LiveData.java:131)