AcademyRepository implements AcademyDataSource { @Override public LiveData<Resource<List<ModuleEntity>>> getAllModulesByCourse(String courseId) { return new NetworkBoundResource<List<ModuleEntity>, List<ModuleResponse>>(appExecutors) { @Override protected LiveData<List<ModuleEntity>> loadFromDB() { return localDataSource.getAllModulesByCourse(courseId); } @Override protected Boolean shouldFetch(List<ModuleEntity> modules) { return (modules == null) || (modules.size() == 0); } @Override protected LiveData<ApiResponse<List<ModuleResponse>>> createCall() { return remoteDataSource.getModules(courseId); } @Override protected void saveCallResult(List<ModuleResponse> moduleResponses) { ArrayList<ModuleEntity> moduleList = new ArrayList<>(); for (ModuleResponse response : moduleResponses) { ModuleEntity course = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); moduleList.add(course); } localDataSource.insertModules(moduleList); } }.asLiveData(); } private  AcademyRepository(@NonNull RemoteDataSource remoteDataSource, @NonNull LocalDataSource localDataSource, AppExecutors appExecutors); static AcademyRepository getInstance(RemoteDataSource remoteData, LocalDataSource localDataSource, AppExecutors appExecutors); @Override LiveData<Resource<List<CourseEntity>>> getAllCourses(); @Override LiveData<List<CourseEntity>> getBookmarkedCourses(); @Override LiveData<Resource<CourseWithModule>> getCourseWithModules(final String courseId); @Override LiveData<Resource<List<ModuleEntity>>> getAllModulesByCourse(String courseId); @Override LiveData<Resource<ModuleEntity>> getContent(String moduleId); @Override void setCourseBookmark(CourseEntity course, boolean state); @Override void setReadModule(ModuleEntity module);  }
@Test public void getAllModulesByCourse() { doAnswer(invocation -> { ((RemoteDataSource.LoadModulesCallback) invocation.getArguments()[1]) .onAllModulesReceived(moduleResponses); return null; }).when(remote).getModules(eq(courseId), any(RemoteDataSource.LoadModulesCallback.class)); List<ModuleEntity> courseEntities = LiveDataTestUtil.getValue(academyRepository.getAllModulesByCourse(courseId)); verify(remote).getModules(eq(courseId), any(RemoteDataSource.LoadModulesCallback.class)); assertNotNull(courseEntities); assertEquals(moduleResponses.size(), courseEntities.size()); }