Search in sources :

Example 1 with MockRetrofit

use of retrofit2.mock.MockRetrofit 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 2 with MockRetrofit

use of retrofit2.mock.MockRetrofit 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

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 NetworkBehavior (retrofit2.mock.NetworkBehavior)1