### Question:
JsonStringParser { public T parseAndValidate(String jsonContent) throws JsonParseException { JsonParser jsonParser = null; try { jsonParser = jsonFactory.createParser(jsonContent); jsonParser.enable(Feature.ALLOW_COMMENTS); T item = jsonParser.readValueAs(type); commonValidator.validateEx(item); return item; } catch (IOException e) { throw new JsonParseException(e.getMessage(), jsonParser!=null?jsonParser.getCurrentLocation():null, e); } } JsonStringParser(Class<T> type); T parseAndValidate(String jsonContent); @Autowired void setCommonValidator(CommonValidator commonValidator); @Autowired void setJsonFactory(JsonFactory jsonFactory);  }### Answer:
@Test public void parseAndValidate() throws IOException { String jsonContent = "232ejfjkdjf"; String parseResult = "233efjdjkfvdkjf"; when(jsonFactory.createParser(jsonContent)).thenReturn(jsonParser); when(jsonParser.readValueAs(String.class)).thenReturn(parseResult); String retValue = jsonStringParser.parseAndValidate(jsonContent); assertEquals(retValue, parseResult); verify(jsonParser).enable(Feature.ALLOW_COMMENTS); verify(commonValidator).validateEx(parseResult); }
@Test(expected = ValidationException.class) public void parseAndValidate_ValidationException() throws IOException { String jsonContent = "232ejfjkdjf"; String parseResult = "233efjdjkfvdkjf"; when(jsonFactory.createParser(jsonContent)).thenReturn(jsonParser); when(jsonParser.readValueAs(String.class)).thenReturn(parseResult); Mockito.doThrow(ValidationException.class).when(commonValidator).validateEx(parseResult); jsonStringParser.parseAndValidate(jsonContent); }
@SuppressWarnings("unchecked") @Test(expected = JsonParseException.class) public void parseAndValidate_JsonParseException() throws IOException { String jsonContent = "232ejfjkdjf"; when(jsonFactory.createParser(jsonContent)).thenReturn(jsonParser); when(jsonParser.readValueAs(String.class)).thenThrow(IOException.class); jsonStringParser.parseAndValidate(jsonContent); }