I’m using rest-assured and twitter4j for testing twitter API.
All calls are made via RestAssured, twitter4j is used for Json response deserialization.
And what I want to do is to deserialize Json response from twitter – GET statuses/home_timeline
which returns Array of Status objects(from twitter4j).
I can easily deserialize one Status object like here:
@Test
public void verifyTwitCreation() {
RequestSpecification spec = new RqBuilder()
.withStatus(textToPublish)
.build();
Response response = twitClient.createTwit(spec);
assertResponseCode(response, 200);
String json = response.getBody().asString();
Status status = null;
try {
status = TwitterObjectFactory.createStatus(json);
} catch (TwitterException e) {
e.printStackTrace();
}
System.out.println(status.toString());
}
But I don’t know how to do the same for deserializing array of such Status objects.
2
Answers
I kinda solve my problem. I just generated POJOs from Json with this plugin - https://plugins.jetbrains.com/plugin/8634-robopojogenerator and than mapped Json with rest-assured
But still I will appreciate the answer for solution with using twitter4j
Try extracting a list of statuses using JsonPath and then parse them using
TwitterObjectFactory
:You could move try/catch during parsing to a separate method so it looks nicer:
Update:
Since JsonPath getList() returns list of maps we should convert all maps to JSON string so that it can be used by TwitterObjectFactory. Jackson’s ObjectMapper is used in example, but any JSON parsing tool can be used.