Forked from Aesthetikx/retrofit_callback_testing.java
Last active
August 29, 2015 14:25
-
-
Save vgaidarji/609719adfeefd96e6e55 to your computer and use it in GitHub Desktop.
Revisions
-
Aesthetikx revised this gist
Jan 16, 2015 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -14,7 +14,7 @@ public void setStatusCode(int statusCode) { this.statusCode = statusCode; } public void setResponseBody(String responseBody) { this.responseBody = responseBody; } -
Aesthetikx revised this gist
Jan 16, 2015 . 1 changed file with 4 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 WidgetApi api; private MockRetrofitClient client; private Widget widget; @@ -43,11 +43,13 @@ public void setUp() { Executor executor = Executors.newSingleThreadExecutor(); 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 { -
Aesthetikx renamed this gist
Jan 15, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Aesthetikx renamed this gist
Jan 15, 2015 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
Aesthetikx created this gist
Jan 15, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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()); } }