Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active September 25, 2016 10:52
Show Gist options
  • Save dherges/dd58cb99597301b376f138d0795fe627 to your computer and use it in GitHub Desktop.
Save dherges/dd58cb99597301b376f138d0795fe627 to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
/**
* Retrofit interface for Twitter REST API
*/
public interface TwitterApi {
@GET("statuses/home_timeline.json")
Call<ResponseBody> homeTimeline();
@GET("statuses/show/:id")
Call<ResponseBody> show(@Path("id") String id);
@POST("statuses/update")
Call<ResponseBody> tweet(@Query("status") String status);
class Builder {
private String baseUrl;
public Builder() {}
public Builder baseUrl(String baseUrl) {
this.baseUrl = baseUrl;
return this;
}
public TwitterApi build() {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.build()
.create(TwitterApi.class);
}
}
}
public class TwitterApiTest {
@Rule
public MockWebServerPlus server = new MockWebServerPlus();
@Test
public void homeTimeline() throws IOException, InterruptedException {
// replay scripted http response from yaml file
server.enqueue("twitter/statuses/home_timeline");
// execute request
final TwitterApi twitterApi = new TwitterApi.Builder()
.baseUrl(server.url("/"))
.build();
final Response<ResponseBody> response = twitterApi.homeTimeline().execute();
// assert response
assertThat(response.code()).isEqualTo(200);
assertThat(response.body().string()).startsWith("[\n {\n \"coordinates\"");
// verify request
final RecordedRequest recordedRequest = server.takeRequest();
assertThat(recordedRequest.getMethod()).isEqualTo("GET");
assertThat(recordedRequest.getPath()).isEqualTo("/statuses/home_timeline.json");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment