Search in sources :

Example 1 with GitHub

use of com.example.retrofit.SimpleService.GitHub 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)

Aggregations

GitHub (com.example.retrofit.SimpleService.GitHub)1 Retrofit (retrofit2.Retrofit)1 MockRetrofit (retrofit2.mock.MockRetrofit)1 NetworkBehavior (retrofit2.mock.NetworkBehavior)1