SplashViewModel extends ViewModel { @Override protected void onCleared() { disposables.clear(); finishScreen.onComplete(); progressing.onComplete(); citiesCount.onComplete(); super.onCleared(); } SplashViewModel(); @RestrictTo(RestrictTo.Scope.TESTS)  SplashViewModel(Scheduler scheduler);   }
@Test public void onClearedThenCloseAllStreams() { TestScheduler scheduler = new TestScheduler(); SplashViewModel viewModel = new SplashViewModel(scheduler); viewModel.finishScreen.onNext(false); viewModel.citiesCount.onNext(1); viewModel.progressing.onNext(false); viewModel.onCleared(); scheduler.triggerActions(); Observable.just(viewModel.getClass()) .map(Class::getDeclaredFields) .flatMap(Observable::fromArray) .doOnNext(field -> field.setAccessible(true)) .filter(field -> field.getType().isAssignableFrom(BehaviorSubject.class)) .map(field -> field.get(viewModel)) .cast(BehaviorSubject.class) .map(subject -> subject.getValue() == null) .defaultIfEmpty(false) .all(Boolean::booleanValue) .onErrorReturnItem(false) .subscribe((Consumer<Boolean>) Assert::assertTrue); }