jon*_*077 45 java junit compare text-files
我正在比较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());
}
Run Code Online (Sandbox Code Playgroud)
这是比较文本文件的好方法吗?什么是首选?
Jon*_*nik 43
这是检查文件是否完全相同的一种简单方法:
assertEquals("The files differ!",
FileUtils.readFileToString(file1, "utf-8"),
FileUtils.readFileToString(file2, "utf-8"));
Run Code Online (Sandbox Code Playgroud)
其中file1
和file2
是File
实例,FileUtils
来自Apache Commons IO.
没有太多自己的代码供您维护,这总是一个加号.:)如果您已经在项目中使用Apache Commons,那么非常简单.但没有像mark的解决方案那样好的,详细的错误消息.
编辑:
嘿,仔细看看FileUtils
API,有一个更简单的方法:
assertTrue("The files differ!", FileUtils.contentEquals(file1, file2));
Run Code Online (Sandbox Code Playgroud)
作为奖励,此版本适用于所有文件,而不仅仅是文本.
IAd*_*ter 31
junit-addons对它有很好的支持:FileAssert
它为您提供以下例外:
junitx.framework.ComparisonFailure: aa Line [3] expected: [b] but was:[a]
Run Code Online (Sandbox Code Playgroud)
Bog*_*mac 14
截至2015年,我会推荐AssertJ,一个优雅而全面的断言库.对于文件,您可以断言另一个文件:
@Test
public void file() {
File actualFile = new File("actual.txt");
File expectedFile = new File("expected.txt");
assertThat(actualFile).hasSameContentAs(expectedFile);
}
Run Code Online (Sandbox Code Playgroud)
或者反对内联字符串:
@Test
public void inline() {
File actualFile = new File("actual.txt");
assertThat(linesOf(actualFile)).containsExactly(
"foo 1",
"foo 2",
"foo 3"
);
}
Run Code Online (Sandbox Code Playgroud)
失败消息也非常有用.如果一条线不同,你得到:
java.lang.AssertionError:
File:
<actual.txt>
and file:
<expected.txt>
do not have equal content:
line:<2>,
Expected :foo 2
Actual :foo 20
Run Code Online (Sandbox Code Playgroud)
如果其中一个文件包含更多行:
java.lang.AssertionError:
File:
<actual.txt>
and file:
<expected.txt>
do not have equal content:
line:<4>,
Expected :EOF
Actual :foo 4
Run Code Online (Sandbox Code Playgroud)
我建议使用Assert.assertThat和一个hamcrest匹配器(junit 4.5或更高版本 - 甚至可能是4.4).
我最终会得到类似的东西:
assertThat(fileUnderTest, containsExactText(expectedFile));
Run Code Online (Sandbox Code Playgroud)
我的匹配器在哪里:
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;
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
Matcher职业选手:
缺点:
FileUtils
肯定是一个很好的.这是检查文件是否完全相同的另一种简单方法.
assertEquals(FileUtils.checksumCRC32(file1), FileUtils.checksumCRC32(file2));
Run Code Online (Sandbox Code Playgroud)
虽然assertEquals()确实提供了比assertTrue()更多的反馈,但checksumCRC32()的结果很长.所以,这可能没有内在的帮助.
Simpel使用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);
Run Code Online (Sandbox Code Playgroud)
或者,如果您想比较各个行:
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));
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
47596 次 |
最近记录: |