Implementation & Testing

Frequently used libs

Mocking Snippets

  • Turning a JSON resource into an object list

    • libs: Gson, Guava, commons-io

  public static ImmutableList<Person> getPeople() throws IOException {
    InputStream inputStream = Resources.getResource("people.json").openStream();
    String json = IOUtils.toString(inputStream);
    Type listType = new TypeToken<ArrayList<Person>>() {
    }.getType();
    List<Person> people = new Gson().fromJson(json, listType);
    return ImmutableList.copyOf(people);
  }
  • Turning a YAML resource into an object list

    • lib: jackson-dataformat-yaml, Guava, commons-io

    • For POJO to work with ObjectMapper, either below is needed:

      • A default constructor

      • Using @JsonCreator, @JsonProperty for constructor and its arguments

public static ImmutableList<Person> getPeopleByYml() throws IOException {
  InputStream inputStream = Resources.getResource("people.yml").openStream();
  ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
  List<Person> people = mapper.readValue(inputStream, new TypeReference<List<Person>>(){});
  return ImmutableList.copyOf(people);
}

Last updated