MoodleViewModel extends AndroidViewModel { public LiveData<RemoteResource<List<MoodleCourse>>> getCourses() { if (courses == null || courses.getValue() == null || courses.getValue().data == null) { courses = repository.getCourses(); } return courses; } MoodleViewModel(Application application, MoodleRepository moodleRepository); boolean isShowCaseHasBeenDisplayed(); void setShowCaseHasBeenDisplayed(boolean display); LiveData<RemoteResource<List<MoodleCourse>>> getCourses(); LiveData<RemoteResource<List<MoodleAssignmentCourse>>> getAssignmentCourses(); LiveData<RemoteResource<MoodleAssignmentSubmission>> getAssignmentSubmission(int assignId); void setDisplayPastAssignments(boolean display); boolean isDisplayPastAssignments(); void setAssignmentsSortIndex(int index); int getAssignmentsSortIndex(); Comparator<MoodleAssignment> getAssignmentsSortComparator(); void selectAssignment(MoodleAssignment selectedAssignment); LiveData<MoodleAssignment> getSelectedAssignment(); static final int SORT_BY_DATE; static final int SORT_ALPHA; }
@Test public void getCourses() { MutableLiveData<RemoteResource<List<MoodleCourse>>> liveData = new MutableLiveData<>(); when(repository.getCourses()).thenReturn(liveData); Observer<RemoteResource<List<MoodleCourse>>> observer = mock(Observer.class); viewModel.getCourses().observeForever(observer); verify(observer, never()).onChanged(any(RemoteResource.class)); RemoteResource<List<MoodleCourse>> remoteRes = RemoteResource.loading(null); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); reset(observer); remoteRes = RemoteResource.success(new MoodleCourses()); liveData.setValue(remoteRes); verify(observer).onChanged(remoteRes); }