ListFileObservable { public Observable<File> create(final File rootDirectory) { return Observable.create(new ObservableOnSubscribe<File>() { @Override public void subscribe(@NonNull ObservableEmitter<File> e) throws Exception { File[] childDirectories = rootDirectory.listFiles(); for (File child : childDirectories) { e.onNext(child); } e.onComplete(); } }); }  Observable<File> create(final File rootDirectory);  }
@Test public void shouldReturnAllChildFiles() throws Exception { File stubRootDirectory = mock(File.class); File mockFile1 = mock(File.class); File mockFile2 = mock(File.class); File mockFile3 = mock(File.class); File childrenContent[] = {mockFile1, mockFile2, mockFile3}; when(stubRootDirectory.listFiles()).thenReturn(childrenContent); sut.create(stubRootDirectory) .subscribeOn(Schedulers.trampoline()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(mockObserver); verify(mockObserver).onNext(mockFile1); verify(mockObserver).onNext(mockFile1); verify(mockObserver).onNext(mockFile3); verify(mockObserver, times(3)).onNext(any(File.class)); verify(mockObserver).onComplete(); }