首页 > 解决方案 > 我如何测试保存方法?

问题描述

我有以下保存方法,但我不知道如何验证此方法。我如何在 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();
    }
}

标签: javajunitjunit4

解决方案


您可以将参考的、预期的输出文件存储在磁盘上,然后将测试的输出与其进行比较。有很多方法可以进行这种比较,包括一些JUnit 插件(特别是它的FileAssert),或者只是将两个文件读入字节数组并断言它们相等。

存在许多其他实用程序,其中一些列在此答案中:文件比较实用程序


推荐阅读