将文本文件与 Junit 进行比较
我正在使用 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());
}
这是比较文本文件的好方法吗? 什么是首选?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
这是检查文件是否完全相同的一种简单方法:
其中
file1
和file2
是File
实例,和FileUtils
来自 Apache Commons IO。没有太多自己的代码需要维护,这始终是一个优点。 :) 如果您已经在项目中使用了 Apache Commons,那么非常简单。 但没有像 mark 的解决方案中那样的详细错误消息。
编辑:
呵呵,仔细观察
FileUtils
API,甚至还有一个 更简单的方法:作为奖励,此版本适用于所有文件,而不仅仅是文本。
Here's one simple approach for checking if the files are exactly the same:
Where
file1
andfile2
areFile
instances, andFileUtils
is from Apache Commons IO.Not much own code for you to maintain, which is always a plus. :) And very easy if you already happen to use Apache Commons in your project. But no nice, detailed error messages like in mark's solution.
Edit:
Heh, looking closer at the
FileUtils
API, there's an even simpler way:As a bonus, this version works for all files, not just text.
junit-addons 对此有很好的支持:FileAssert
它为您提供了如下例外:
junit-addons has nice support for it: FileAssert
It gives you exceptions like:
以下是各种第三方 Java 库中文件比较器的更详尽列表:
Here is a more exhaustive list of File comparator's in various 3rd-party Java libraries:
截至 2015 年,我会推荐 AssertJ,一个优雅而全面的断言库。 对于文件,您可以针对另一个文件进行断言:
或针对内联字符串进行断言:
失败消息也非常有用。 如果一行不同,您将得到:
如果其中一个文件有多行,您将得到:
As of 2015, I would recomment AssertJ, an elegant and comprehensive assertion library. For files, you can assert against another file:
or against inline strings:
The failure messages are very informative as well. If a line is different, you get:
and if one of the files has more lines you get:
使用
java.nio.file
API 简单比较两个文件的内容。或者,如果您想比较各个行:
Simple comparison of the content of two files with
java.nio.file
API.Or if you want to compare individual lines:
我建议使用 Assert.assertThat 和 hamcrest 匹配器(junit 4.5 或更高版本 - 甚至可能4.4)。
我最终会得到类似的结果:
我的匹配器在哪里:
匹配器优点:
缺点:
I'd suggest using Assert.assertThat and a hamcrest matcher (junit 4.5 or later - perhaps even 4.4).
I'd end up with something like:
where my matcher is:
Matcher pros:
Cons:
FileUtils
确实是一个不错的选择。 这是另一个 检查文件是否完全相同的简单方法。虽然assertEquals()确实比assertTrue()提供了更多的反馈,但checksumCRC32()的结果很长。 所以,这可能没有什么本质上的帮助。
FileUtils
sure is a good one. Here's yet another simple approach for checking if the files are exactly the same.While the assertEquals() does provide a little more feedback than the assertTrue(), the result of checksumCRC32() is a long. So, that may not be intrisically helpful.
如果预期的行数比实际的行数多,则在稍后进入assertNull 之前,您将失败assertEquals。
不过修复起来相当容易:
If expected has more lines than actual, you'll fail an assertEquals before getting to the assertNull later.
It's fairly easy to fix though:
这是我自己的
equalFiles
实现,无需向您的项目添加任何库。要使用它,只需使用常规的
AssertTrue
JUnit 方法This is my own implementation of
equalFiles
, no need to add any library to your project.And to use it just use regular
AssertTrue
JUnit method