我正在为抛出JsonGenerationException,JsonMappingException,IOException和Exception的方法编写测试用例。
我该如何创建一个代码,在该代码中抛出JsonGenerationException以及列出的异常以外的其他任何异常
方法:-
public static String convertToJson(Object response) {
ObjectMapper mapper = new ObjectMapper();
try {
return mapper.writeValueAsString(response);
} catch (JsonGenerationException e) {
Logger.error("convertToJson JsonGenerationException Occured" + e);
} catch (JsonMappingException e) {
Logger.error("convertToJson JsonMappingException Occured" + e);
} catch (IOException e) {
Logger.error("convertToJson IOException Occured" + e );
} catch (Exception e) {
Logger.error("convertToJson Exception Occured" + e );
}
return null;
}
测试用例
@Test
public void testConvertToJsonCase3() throws JsonParseException, IOException {
JsonParser jp = new JsonFactory().createParser("{ \"foo\": \"bar\" }");
String result = MyUtil.convertToJson(jp);
assertNull(result);
}
@Test
public void testConvertToJsonCase4() {
Object mockItem = mock(Object.class);
when(mockItem.toString()).thenReturn(mockItem.getClass().getName());
String result = MyUtil.convertToJson(mockIte`enter code here`m);
assertNull(result);
}