GitHubInteractor { public Observable<SearchResult> searchUsers(final String query) { return Observable.concat(cachedResults(query), networkResults(query)).first(); } GitHubInteractor(Retrofit retrofit, LruCache<String, SearchResult> cache); Observable<SearchResult> searchUsers(final String query);  }
@Test public void mockedResponseTest() { SearchResult result = new SearchResult(); result.setTotalCount(1); SearchItem item = new SearchItem(); item.setLogin("octocat"); item.setAvatarUrl("https: result.setItems(Collections.singletonList(item)); MockWebServer server = new MockWebServer(); server.enqueue(new MockResponse().setBody(new Gson().toJson(result))); Retrofit retrofit = new Retrofit.Builder() .baseUrl(server.url("/search/repositories?q=octocat")) .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); TestSubscriber<SearchResult> subscriber = new TestSubscriber<>(); new GitHubInteractor(retrofit, cache).searchUsers("octocat").subscribe(subscriber); subscriber.assertValue(result); subscriber.assertNoErrors(); subscriber.assertCompleted(); }
@Test public void realResponseTest() { SearchItem item = new SearchItem(); item.setLogin("octocat"); item.setAvatarUrl("https: Retrofit retrofit = new Retrofit.Builder() .baseUrl("https: .addCallAdapterFactory(RxJavaCallAdapterFactory.create()) .addConverterFactory(GsonConverterFactory.create()) .build(); TestSubscriber<SearchItem> subscriber = new TestSubscriber<>(); new GitHubInteractor(retrofit, cache).searchUsers("octocat") .flatMap(new Func1<SearchResult, Observable<SearchItem>>() { @Override public Observable<SearchItem> call(SearchResult result) { return Observable.from(result.getItems()); } }) .first() .toBlocking() .subscribe(subscriber); subscriber.assertValue(item); subscriber.assertNoErrors(); subscriber.assertCompleted(); }