AndroidPlacesApiJsonParser implements PlacesApiJsonParser { List<MatchedSubstring> readMatchedSubstringsArray(JsonReader reader) throws IOException { List<MatchedSubstring> matchedSubstrings = new ArrayList<>(); reader.beginArray(); while (reader.hasNext()) { int length = -1; int offset = -1; reader.beginObject(); while (reader.hasNext()) { switch (reader.nextName()) { case "length": length = reader.nextInt(); break; case "offset": offset = reader.nextInt(); break; default: reader.skipValue(); break; } } reader.endObject(); matchedSubstrings.add(new MatchedSubstring(length, offset)); } reader.endArray(); return matchedSubstrings; }  @Override PlacesAutocompleteResponse autocompleteFromStream(final InputStream is); @Override PlacesDetailsResponse detailsFromStream(final InputStream is); @Override List<Place> readHistoryJson(final InputStream in); @Override void writeHistoryJson(final OutputStream os, final List<Place> places);  }
@Test public void readMatchedSubstringsArrayTest() throws IOException { when(parser.readMatchedSubstringsArray(reader)).thenCallRealMethod(); when(reader.hasNext()).thenReturn(true, true, true, true, false, true, false, false); when(reader.nextName()).thenReturn("length", "offset", "skip"); when(reader.nextInt()).thenReturn(1, 2); List<MatchedSubstring> expected = Arrays.asList(new MatchedSubstring(1, 2), new MatchedSubstring(-1, -1)); List<MatchedSubstring> actual = parser.readMatchedSubstringsArray(reader); assertEquals(expected, actual); }