Skip to content

Instantly share code, notes, and snippets.

@dherges
Last active September 25, 2016 12:38
Show Gist options
  • Select an option

  • Save dherges/3fdc0b177b0a036f29e370f101f9eaac to your computer and use it in GitHub Desktop.

Select an option

Save dherges/3fdc0b177b0a036f29e370f101f9eaac to your computer and use it in GitHub Desktop.
ok-testing-reloaded-medium
/**
* Representation of a Tweet object transferred from/to Twitter REST API in JSON format.
*/
public class Tweet {
public long id;
public String text;
public User user;
@Json(name="created_at") public Date createdAt;
public boolean favorited;
}
/**
* Retrofit interface for Twitter REST API
*/
public interface TwitterApi {
@GET("statuses/home_timeline.json")
Call<List<Tweet>> homeTimeline();
@GET("statuses/show/{id}")
Call<Tweet> show(@Path("id") String id);
@POST("statuses/update")
Call<Tweet> tweet(@Query("status") String status);
class Builder {
/* ... */
public TwitterApi build() {
return new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(MoshiConverterFactory.create(
new Moshi.Builder()
.add(Date.class, new DateJsonAdapter("EEE MMM dd kk:mm:ss z yyyy"))
.build()
))
.build()
.create(TwitterApi.class);
}
}
}
public class TwitterApiTest {
@Test
public void homeTimeline() throws IOException, InterruptedException {
/* ... */
// execute request
final Response<List<Tweet>> response = twitterApi.homeTimeline().execute();
// assert response
assertThat(response.body().get(0).createdAt).isEqualTo("2012-08-28T23:16:23.000");
assertThat(response.body().get(0).user.name).isEqualTo("OAuth Dancer");
/* ... */
}
}
/**
* Representation of an User object transferred from/to Twitter REST API in JSON format.
*/
public class User {
public String name;
@Json(name = "created_at") public Date createdAt;
public String location;
public String url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment