AcademyRepository implements AcademyDataSource { @Override public LiveData<List<ModuleEntity>> getAllModulesByCourse(String courseId) { MutableLiveData<List<ModuleEntity>> moduleResults = new MutableLiveData<>(); remoteDataSource.getModules(courseId, 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); } moduleResults.postValue(moduleList); }); return moduleResults; } 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 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()); }
@Test public void getAllModulesByCourse() { when(remote.getModules(courseId)).thenReturn(moduleResponses); ArrayList<ModuleEntity> moduleEntities = academyRepository.getAllModulesByCourse(courseId); verify(remote).getModules(courseId); assertNotNull(moduleEntities); assertEquals(moduleResponses.size(), moduleEntities.size()); }