AcademyRepository implements AcademyDataSource { @Override public LiveData<CourseEntity> getCourseWithModules(final String courseId) { MutableLiveData<CourseEntity> courseResult = new MutableLiveData<>(); remoteDataSource.getAllCourses(courseResponses -> { CourseEntity course = null; for (CourseResponse response : courseResponses) { if (response.getId().equals(courseId)) { course = new CourseEntity(response.getId(), response.getTitle(), response.getDescription(), response.getDate(), false, response.getImagePath()); } } courseResult.postValue(course); }); return courseResult; } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource); static AcademyRepository getInstance(RemoteDataSource remoteData); @Override LiveData<List<CourseEntity>> getAllCourses(); @Override LiveData<List<CourseEntity>> getBookmarkedCourses(); @Override LiveData<CourseEntity> getCourseWithModules(final String courseId); @Override LiveData<List<ModuleEntity>> getAllModulesByCourse(String courseId); @Override LiveData<ModuleEntity> getContent(String courseId, String moduleId);  }
@Test public void getCourseWithModules() { when(remote.getAllCourses()).thenReturn(courseResponses); CourseEntity resultCourse = academyRepository.getCourseWithModules(courseId); verify(remote).getAllCourses(); assertNotNull(resultCourse); assertEquals(courseResponses.get(0).getTitle(), resultCourse.getTitle()); }
@Test public void getCourseWithModules() { doAnswer(invocation -> { ((RemoteDataSource.LoadCoursesCallback) invocation.getArguments()[0]) .onAllCoursesReceived(courseResponses); return null; }).when(remote).getAllCourses(any(RemoteDataSource.LoadCoursesCallback.class)); CourseEntity courseEntities = LiveDataTestUtil.getValue(academyRepository.getCourseWithModules(courseId)); verify(remote).getAllCourses(any(RemoteDataSource.LoadCoursesCallback.class)); assertNotNull(courseEntities); assertNotNull(courseEntities.getTitle()); assertEquals(courseResponses.get(0).getTitle(), courseEntities.getTitle()); }