50

我正在使用以下方法比较 junit 中的文本文件:

public static void assertReaders(BufferedReader expected,
          BufferedReader actual) throws IOException {
    String line;
    while ((line = expected.readLine()) != null) {
        assertEquals(line, actual.readLine());
    }

    assertNull("Actual had more lines then the expected.", actual.readLine());
    assertNull("Expected had more lines then the actual.", expected.readLine());
}

这是比较文本文件的好方法吗?什么是首选?

4

9 回答 9

47

这是检查文件是否完全相同的一种简单方法:

assertEquals("The files differ!", 
    FileUtils.readFileToString(file1, "utf-8"), 
    FileUtils.readFileToString(file2, "utf-8"));

Wherefile1file2are Fileinstances,并且FileUtils来自Apache Commons IO

没有太多自己的代码供您维护,这总是一个优点。:) 如果您已经碰巧在您的项目中使用了 Apache Commons,那么非常容易。但没有像马克的解决方案那样好的、详细的错误消息。

编辑
嘿,仔细看看FileUtilsAPI,还有一种更简单的方法

assertTrue("The files differ!", FileUtils.contentEquals(file1, file2));

作为奖励,此版本适用于所有文件,而不仅仅是文本。

于 2009-01-21T21:32:51.287 回答
31

junit-addons对它有很好的支持:FileAssert

它为您提供以下例外:

junitx.framework.ComparisonFailure: aa Line [3] expected: [b] but was:[a]
于 2009-01-21T20:36:43.863 回答
18

截至 2015 年,我会推荐AssertJ,这是一个优雅而全面的断言库。对于文件,您可以针对另一个文件进行断言:

@Test
public void file() {
    File actualFile = new File("actual.txt");
    File expectedFile = new File("expected.txt");
    assertThat(actualFile).hasSameContentAs(expectedFile);
}

或针对内联字符串:

@Test
public void inline() {
    File actualFile = new File("actual.txt");
    assertThat(linesOf(actualFile)).containsExactly(
            "foo 1",
            "foo 2",
            "foo 3"
    );
}

失败消息也非常有用。如果一行不同,你会得到:

java.lang.AssertionError: 
File:
  <actual.txt>
and file:
  <expected.txt>
do not have equal content:
line:<2>, 
Expected :foo 2
Actual   :foo 20

如果其中一个文件有更多行,您会得到:

java.lang.AssertionError:
File:
  <actual.txt>
and file:
  <expected.txt>
do not have equal content:
line:<4>,
Expected :EOF
Actual   :foo 4
于 2015-03-15T16:03:53.697 回答
8

java.nio.file用API简单比较两个文件的内容。

byte[] file1Bytes = Files.readAllBytes(Paths.get("Path to File 1"));
byte[] file2Bytes = Files.readAllBytes(Paths.get("Path to File 2"));

String file1 = new String(file1Bytes, StandardCharsets.UTF_8);
String file2 = new String(file2Bytes, StandardCharsets.UTF_8);

assertEquals("The content in the strings should match", file1, file2);

或者,如果您想比较各个行:

List<String> file1 = Files.readAllLines(Paths.get("Path to File 1"));
List<String> file2 = Files.readAllLines(Paths.get("Path to File 2"));

assertEquals(file1.size(), file2.size());

for(int i = 0; i < file1.size(); i++) {
   System.out.println("Comparing line: " + i)
   assertEquals(file1.get(i), file2.get(i));
}
于 2016-03-30T13:01:42.810 回答
7

我建议使用 Assert.assertThat 和hamcrest 匹配器(junit 4.5 或更高版本 - 甚至可能是 4.4)。

我最终会得到类似的东西:

assertThat(fileUnderTest, containsExactText(expectedFile));

我的匹配器在哪里:

class FileMatcher {
   static Matcher<File> containsExactText(File expectedFile){
      return new TypeSafeMatcher<File>(){
         String failure;
         public boolean matchesSafely(File underTest){
            //create readers for each/convert to strings
            //Your implementation here, something like:
              String line;
              while ((line = expected.readLine()) != null) {
                 Matcher<?> equalsMatcher = CoreMatchers.equalTo(line);
                 String actualLine = actual.readLine();
                 if (!equalsMatcher.matches(actualLine){
                    failure = equalsMatcher.describeFailure(actualLine);
                    return false;
                 }
              }
              //record failures for uneven lines
         }

         public String describeFailure(File underTest);
             return failure;
         }
      }
   }
}

匹配器优点:

  • 组合和重用
  • 在普通代码和测试中使用
    • 收藏品
    • 用于模拟框架
    • 可以使用一般谓词函数
  • 非常好的日志能力
  • 可与其他匹配器结合,描述和故障描述准确准确

缺点:

  • 好吧,这很明显对吧?这比 assert 或 junitx 更冗长(对于这种特殊情况)
  • 您可能需要包含 hamcrest 库才能获得最大收益
于 2009-01-21T21:01:04.067 回答
5

FileUtils肯定是一个很好的。这是检查文件是否完全相同的另一种简单方法。

assertEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));

虽然 assertEquals() 确实提供了比 assertTrue() 更多的反馈,但 checksumCRC32() 的结果很长。所以,这可能没有本质上的帮助。

于 2011-01-13T18:15:52.317 回答
4

如果预期的行数多于实际行数,您将在稍后到达 assertNull 之前使 assertEquals 失败。

不过,这很容易修复:

public static void assertReaders(BufferedReader expected,
    BufferedReader actual) throws IOException {
  String expectedLine;
  while ((expectedLine = expected.readLine()) != null) {
    String actualLine = actual.readLine();
    assertNotNull("Expected had more lines then the actual.", actualLine);
    assertEquals(expectedLine, actualLine);
  }
  assertNull("Actual had more lines then the expected.", actual.readLine());
}
于 2009-01-21T20:31:20.460 回答
0

这是我自己的实现equalFiles,不需要在你的项目中添加任何库。

private static boolean equalFiles(String expectedFileName,
        String resultFileName) {
    boolean equal;
    BufferedReader bExp;
    BufferedReader bRes;
    String expLine ;
    String resLine ;

    equal = false;
    bExp = null ;
    bRes = null ;

    try {
        bExp = new BufferedReader(new FileReader(expectedFileName));
        bRes = new BufferedReader(new FileReader(resultFileName));

        if ((bExp != null) && (bRes != null)) {
            expLine = bExp.readLine() ;
            resLine = bRes.readLine() ;

            equal = ((expLine == null) && (resLine == null)) || ((expLine != null) && expLine.equals(resLine)) ;

            while(equal && expLine != null)
            {
                expLine = bExp.readLine() ;
                resLine = bRes.readLine() ; 
                equal = expLine.equals(resLine) ;
            }
        }
    } catch (Exception e) {

    } finally {
        try {
            if (bExp != null) {
                bExp.close();
            }
            if (bRes != null) {
                bRes.close();
            }
        } catch (Exception e) {
        }

    }

    return equal;

}

要使用它,只需使用常规的AssertTrueJUnit 方法

assertTrue(equalFiles(expected, output)) ;
于 2015-06-07T05:29:07.517 回答