创建一个JsonProcessingException [英] Create a JsonProcessingException

查看:251
本文介绍了创建一个JsonProcessingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个由模拟对象抛出的JsonProcessingException。

I'm trying to create a JsonProcessingException to be thrown by a mock object.

when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException("Error"));

但是我无法创建JsonProcessingException对象,因为所有构造函数都受到保护。我如何解决这个问题?

However I'm unable to create a JsonProcessingException object as all the constructors are protected. How do I get around this?

推荐答案

如何创建JsonProcessingException类型的匿名异常

how about you create an anonymous exception of type JsonProcessingException

when(mapper.writeValueAsString(any(Object.class))).thenThrow(new JsonProcessingException("Error"){});

{}大括号可以解决问题。这样做要好得多,因为它不会让测试代码的读者感到困惑。

The {} braces does the trick. This is much better since it is not confusing to the reader of the test code.

这篇关于创建一个JsonProcessingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆