AcademyRepository implements AcademyDataSource { @Override public LiveData<ModuleEntity> getContent(String courseId, String moduleId) { MutableLiveData<ModuleEntity> moduleResult = new MutableLiveData<>(); remoteDataSource.getModules(courseId, moduleResponses -> { ModuleEntity module; for (ModuleResponse response : moduleResponses) { if (response.getModuleId().equals(moduleId)) { module = new ModuleEntity(response.getModuleId(), response.getCourseId(), response.getTitle(), response.getPosition(), false); remoteDataSource.getContent(moduleId, contentResponse -> { module.contentEntity = new ContentEntity(contentResponse.getContent()); moduleResult.postValue(module); }); break; } } }); return moduleResult; } 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 getContent() { when(remote.getModules(courseId)).thenReturn(moduleResponses); when(remote.getContent(moduleId)).thenReturn(content); ModuleEntity resultModule = academyRepository.getContent(courseId, moduleId); verify(remote).getContent(moduleId); assertNotNull(resultModule); assertEquals(content.getContent(), resultModule.contentEntity.getContent()); }
@Test public void getContent() { doAnswer(invocation -> { ((RemoteDataSource.LoadModulesCallback) invocation.getArguments()[1]) .onAllModulesReceived(moduleResponses); return null; }).when(remote).getModules(eq(courseId), any(RemoteDataSource.LoadModulesCallback.class)); doAnswer(invocation -> { ((RemoteDataSource.LoadContentCallback) invocation.getArguments()[1]) .onContentReceived(content); return null; }).when(remote).getContent(eq(moduleId), any(RemoteDataSource.LoadContentCallback.class)); ModuleEntity courseEntitiesContent = LiveDataTestUtil.getValue(academyRepository.getContent(courseId, moduleId)); verify(remote) .getModules(eq(courseId), any(RemoteDataSource.LoadModulesCallback.class)); verify(remote) .getContent(eq(moduleId), any(RemoteDataSource.LoadContentCallback.class)); assertNotNull(courseEntitiesContent); assertNotNull(courseEntitiesContent.contentEntity); assertNotNull(courseEntitiesContent.contentEntity.getContent()); assertEquals(content.getContent(), courseEntitiesContent.contentEntity.getContent()); }