junit - 如何使用 JUnit 测试运行时异常场景
问题描述
我有以下需要测试的课程。我可以测试课程,但在这种情况下如何测试异常场景,因为我无法模拟ObjectMapper
课程。
package xyz.util;
import ...
public class JsonConverter {
private static ObjectMapper mapper;
static {
mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter();
}
private JsonConverter() {}
public static String toJson(Object object) {
try {
return mapper.writeValueAsString(object);
} catch (JsonProcessingException e) {
throw new AppplicationRuntimeException("Exception while converting object to JSON", e);
}
}
public static <T> T toObject(String jsonString, Class<T> clazz) {
try {
return mapper.readValue(jsonString, clazz);
} catch (IOException e) {
throw new AppRuntimeException("Exception while converting JSON to Object", e);
}
}
}
解决方案
推荐阅读
- c# - .Net 用于针对 ADLS(Azure 数据湖存储)第 1 代的 Apache Spark 身份验证
- android - 使用对象存储从服务器检索的数据是否有任何问题?
- flutter - Flutter SafeArea 意外隐藏系统图标
- python - 如何从列表 PYTHON 中的字符串中删除字符
- python - 当我使用字符串作为 DataFrame.agg() 的 func 参数时,我如何知道调用了什么函数?
- javascript - 使用 useState Hook 引用函数状态时关闭
- css - Apple Safari 中的 ReactJS 断线
- python - 如何在 python 数据框中的最后一个下划线后删除字符串?
- python - 为什么从 numpy.random 和 scipy.stats 随机生成的分布如此不同以至于它们的概率不能正确求和?
- javascript - Vue 3嵌套路由器中渲染函数和模板之间的不同行为