Payload implements JsonSerializable { @Override public Map<String, Object> asJson() { Map<String, Object> values = new HashMap<>(); if (accessToken != null) { values.put("access_token", accessToken); } if (data != null) { values.put("data", data); } return values; } private  Payload(Builder builder);  Payload(String json); String getAccessToken(); Data getData(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); @Override Map<String, Object> asJson(); final String json; }
@Test public void shouldReturnAsJson() { Payload payload = new Payload.Builder() .accessToken(ACCESS_TOKEN) .data(DATA) .build(); Map<String, Object> expected = new HashMap<>(); expected.put("access_token", ACCESS_TOKEN); expected.put("data", DATA); assertEquals(expected, payload.asJson()); }