Search in sources :

Example 1 with NetworkBehavior

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;
}
Also used : NetworkBehavior(retrofit2.mock.NetworkBehavior) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 2 with NetworkBehavior

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;
}
Also used : NetworkBehavior(retrofit2.mock.NetworkBehavior) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Example 3 with 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();
        }
    };
}
Also used : Retrofit(retrofit2.Retrofit) Call(retrofit2.Call) Before(org.junit.Before)

Example 4 with NetworkBehavior

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");
}
Also used : Retrofit(retrofit2.Retrofit) MockRetrofit(retrofit2.mock.MockRetrofit) MockRetrofit(retrofit2.mock.MockRetrofit) GitHub(com.example.retrofit.SimpleService.GitHub) NetworkBehavior(retrofit2.mock.NetworkBehavior)

Example 5 with NetworkBehavior

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;
}
Also used : NetworkBehavior(retrofit2.mock.NetworkBehavior) Singleton(javax.inject.Singleton) Provides(dagger.Provides)

Aggregations

NetworkBehavior (retrofit2.mock.NetworkBehavior)4 Provides (dagger.Provides)3 Singleton (javax.inject.Singleton)3 Retrofit (retrofit2.Retrofit)2 GitHub (com.example.retrofit.SimpleService.GitHub)1 Before (org.junit.Before)1 Call (retrofit2.Call)1 MockRetrofit (retrofit2.mock.MockRetrofit)1