Skip to content

Instantly share code, notes, and snippets.

@patrykpoborca
Created March 16, 2016 23:34
Show Gist options
  • Save patrykpoborca/c17ecad34faac58fd7f6 to your computer and use it in GitHub Desktop.
Save patrykpoborca/c17ecad34faac58fd7f6 to your computer and use it in GitHub Desktop.
public class ExampleUnitTest {
private boolean mResponseCached = false
public void testCachingStrategy(){
MockManagerDelegate mockDelegate = new MockManagerDelegate();
ApplicationFactory.setInstance(mockDelegate);
mockDelegate.setCachingManager(new CachingManager(){
@Override
void cacheSimpleResponse(Response response) {
mResponseCached = true;
}
});
MyViewModel testingModel = new MyViewModel();
testingModel.retrieveSomeValue("someValue");
Assert.assertTrue(mResponseCached);
}
public class MyViewModel {
private Retrofit retrofit = ApplicationFactory.getRetrofitClient();
private ManagerDelegate managerDelegate = ApplicationFactory.getManagerDelegateImpl();
private Observable<Response> retrieveSomeValue(String param) {
RetrofitClient client = retrofit.create(RetrofitClient.class);
Observable<Response> request = client.doRequest(param);
request.subscribe(response ->{
managerDelegate.getCachingManager().cacheSimpleResponse(response);
});
//by returning the "promise" of this network request we can allow some other component of the app to do the routing for us.
return request;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment