java - 我如何测试保存方法?
问题描述
我有以下保存方法,但我不知道如何验证此方法。我如何在 JUnit 中验证它?
public static void save(Spiel spielen,File file ) {
try(ObjectOutputStream out= new ObjectOutputStream(new FileOutputStream(file))) {
out.writeObject(spielen);
System.out.println("Speichern Erfolgreich");
System.out.println();
}
catch (Exception e) {
System.out.println("Fehler beim Speichern");
System.out.println();
}
}
解决方案
您可以将参考的、预期的输出文件存储在磁盘上,然后将测试的输出与其进行比较。有很多方法可以进行这种比较,包括一些JUnit 插件(特别是它的FileAssert),或者只是将两个文件读入字节数组并断言它们相等。
存在许多其他实用程序,其中一些列在此答案中:文件比较实用程序
推荐阅读
- python - 如果列表被就地修改,则 Traitlets 列表将不会运行回调
- python - Bokeh 未从 CheckboxGroup 更新绘图线更新
- javascript - 使用 JavaScript 将 HTML Canvas 拆分为行
- python - Python 中的 3D BPP 算法
- excel - excel从向下滚动冻结行和前3列
- hadoop - 错误:无法找到或加载主类 org.apache.hadoop.mapred.YarnChild
- eclipse - Birt 条件分页符
- swift - ViewController 滑动动画
- mysql - 通过动态增加拆分在 oracle 中创建拆分分区
- c# - C# ASP.NET Core AutoMapper:子对象为空