Store { Observable<T> get() { if(obj == null) { String objJson = prefs.getString(TAG, null); obj = new Gson().fromJson(objJson, cls); } return obj == null ? Observable.<T>empty():Observable.just(obj); } Store(SharedPreferences prefs, Class<T> cls);   }
@Test public void nullEntityReturnsEmptyObservable() { TestSubscriber<TestEntity> ts = new TestSubscriber<>(); store.get().subscribe(ts); ts.assertNoErrors(); ts.assertCompleted(); }
@Test public void getEntityReturnsPersistedWhenNull() { when(prefs.getString("object", null)).thenReturn(new Gson().toJson(entity)); TestSubscriber<TestEntity> ts = new TestSubscriber<>(); store.get().subscribe(ts); assertThat(ts.getOnNextEvents(), hasItems(entity)); }