Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save vgaidarji/609719adfeefd96e6e55 to your computer and use it in GitHub Desktop.

Select an option

Save vgaidarji/609719adfeefd96e6e55 to your computer and use it in GitHub Desktop.

Revisions

  1. @Aesthetikx Aesthetikx revised this gist Jan 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion retrofit_callback_testing.java
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ public void setStatusCode(int statusCode) {
    this.statusCode = statusCode;
    }

    public vois setResponseBody(String responseBody) {
    public void setResponseBody(String responseBody) {
    this.responseBody = responseBody;
    }

  2. @Aesthetikx Aesthetikx revised this gist Jan 16, 2015. 1 changed file with 4 additions and 2 deletions.
    6 changes: 4 additions & 2 deletions retrofit_callback_testing.java
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ public Response execute(Request request) throws IOException {
    @RunWith(RobolectricGradleTestRunner.class)
    public class WidgetApiTest {

    private RestAdapter adapter;
    private WidgetApi api;
    private MockRetrofitClient client;

    private Widget widget;
    @@ -43,11 +43,13 @@ public void setUp() {

    Executor executor = Executors.newSingleThreadExecutor();

    adapter = new RestAdapter.Builder()
    RestAdapter adapter = new RestAdapter.Builder()
    .setEndpoint("https://mock.com")
    .setClient(client)
    .setExecutors(executor, executor)
    .build();

    api = adapter.create(WidgetApi.class);
    }

    private String loadFixture(String relativePath) throws IOException {
  3. @Aesthetikx Aesthetikx renamed this gist Jan 15, 2015. 1 changed file with 0 additions and 0 deletions.
  4. @Aesthetikx Aesthetikx renamed this gist Jan 15, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  5. @Aesthetikx Aesthetikx created this gist Jan 15, 2015.
    82 changes: 82 additions & 0 deletions gistfile1.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,82 @@
    // WidgetApi.java
    public interface WidgetApi {
    @GET("/widget/{:id}")
    void getWidget(@Path("id") int id, Callback<Widget> callback);
    }

    // MockRetrofitClient.java
    public class MockRetrofitClient implements Client {

    private int statusCode = 200;
    private String responseBody = "{}";

    public void setStatusCode(int statusCode) {
    this.statusCode = statusCode;
    }

    public vois setResponseBody(String responseBody) {
    this.responseBody = responseBody;
    }

    @Override
    public Response execute(Request request) throws IOException {
    return new Response(request.getUrl(),
    statusCode,
    responseBody,
    Collections.EMPTY_LIST,
    new TypedByteArray("application/json", responseBody.getBytes()));
    }
    }

    // WidgetApiTest.java
    @RunWith(RobolectricGradleTestRunner.class)
    public class WidgetApiTest {

    private RestAdapter adapter;
    private MockRetrofitClient client;

    private Widget widget;

    @Before
    public void setUp() {
    client = new MockRetrofitClient();

    Executor executor = Executors.newSingleThreadExecutor();

    adapter = new RestAdapter.Builder()
    .setEndpoint("https://mock.com")
    .setClient(client)
    .setExecutors(executor, executor)
    .build();
    }

    private String loadFixture(String relativePath) throws IOException {
    String path = "src/test/fixtures/" + relativePath;
    return FileUtils.readFileToString(new File(path));
    }

    @Test
    public void testGetWidget() throws Exception {
    client.setResponseBody(loadFixture("/api/widgets/widget-1.json"));
    final CountDownLatch latch = new CountDownLatch(1);

    api.getWidget(1, new Callback<Widget>() {
    @Override
    public void success(Widget widget, Response response) {
    this.widget = widget;
    latch.countDown();
    }

    @Override
    public void failure(RetrofitError error) {
    this.widget = null;
    latch.countDown();
    }
    });

    latch.await();

    assertEquals("test-widget-name", widget.getName());
    }

    }