use of retrofit2.mock.NetworkBehavior in project u2020 by JakeWharton.
the class DebugApiModule method provideBehavior.
@Provides
@Singleton
NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay, @NetworkFailurePercent Preference<Integer> networkFailurePercent, @NetworkVariancePercent Preference<Integer> networkVariancePercent) {
NetworkBehavior behavior = NetworkBehavior.create();
behavior.setDelay(networkDelay.get(), MILLISECONDS);
behavior.setFailurePercent(networkFailurePercent.get());
behavior.setVariancePercent(networkVariancePercent.get());
return behavior;
}
use of retrofit2.mock.NetworkBehavior in project Just-Another-Android-App by athkalia.
the class BuildTypeAwareModule method providesNetworkBehavior.
@Provides
@Singleton
@SuppressWarnings("checkstyle:magicnumber")
public static NetworkBehavior providesNetworkBehavior() {
NetworkBehavior networkBehavior = NetworkBehavior.create();
networkBehavior.setDelay(1, TimeUnit.SECONDS);
// Delay can vary by 50%.
networkBehavior.setVariancePercent(50);
// 20% of all calls are failing.
networkBehavior.setFailurePercent(20);
return networkBehavior;
}
use of retrofit2.mock.NetworkBehavior in project retrofit by square.
the class BehaviorDelegateTest method setUp.
@Before
public void setUp() {
Retrofit retrofit = new Retrofit.Builder().baseUrl("http://example.com").build();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit).networkBehavior(behavior).build();
final BehaviorDelegate<DoWorkService> delegate = mockRetrofit.create(DoWorkService.class);
service = new DoWorkService() {
@Override
public Call<String> response() {
Call<String> response = Calls.response("Response!");
return delegate.returning(response).response();
}
@Override
public Call<String> failure() {
Call<String> failure = Calls.failure(mockFailure);
return delegate.returning(failure).failure();
}
};
}
use of retrofit2.mock.NetworkBehavior in project retrofit by square.
the class SimpleMockService method main.
public static void main(String... args) throws IOException {
// Create a very simple Retrofit adapter which points the GitHub API.
Retrofit retrofit = new Retrofit.Builder().baseUrl(SimpleService.API_URL).build();
// Create a MockRetrofit object with a NetworkBehavior which manages the fake behavior of calls.
NetworkBehavior behavior = NetworkBehavior.create();
MockRetrofit mockRetrofit = new MockRetrofit.Builder(retrofit).networkBehavior(behavior).build();
BehaviorDelegate<GitHub> delegate = mockRetrofit.create(GitHub.class);
MockGitHub gitHub = new MockGitHub(delegate);
// Query for some contributors for a few repositories.
printContributors(gitHub, "square", "retrofit");
printContributors(gitHub, "square", "picasso");
// Using the mock-only methods, add some additional data.
System.out.println("Adding more mock data...\n");
gitHub.addContributor("square", "retrofit", "Foo Bar", 61);
gitHub.addContributor("square", "picasso", "Kit Kat", 53);
// Reduce the delay to make the next calls complete faster.
behavior.setDelay(500, TimeUnit.MILLISECONDS);
// Query for the contributors again so we can see the mock data that was added.
printContributors(gitHub, "square", "retrofit");
printContributors(gitHub, "square", "picasso");
}
use of retrofit2.mock.NetworkBehavior in project u2020 by JakeWharton.
the class DebugApiModule method provideBehavior.
@Provides
@Singleton
NetworkBehavior provideBehavior(@NetworkDelay Preference<Long> networkDelay, @NetworkVariancePercent Preference<Integer> networkVariancePercent, @NetworkFailurePercent Preference<Integer> networkFailurePercent, @NetworkErrorPercent Preference<Integer> networkErrorPercent, Preference<NetworkErrorCode> networkErrorCode) {
NetworkBehavior behavior = NetworkBehavior.create();
behavior.setDelay(networkDelay.get(), MILLISECONDS);
behavior.setVariancePercent(networkVariancePercent.get());
behavior.setFailurePercent(networkFailurePercent.get());
behavior.setErrorPercent(networkErrorPercent.get());
behavior.setErrorFactory(() -> Response.error(networkErrorCode.get().code, ResponseBody.create(null, new byte[0])));
return behavior;
}
Aggregations