Java 如何比较 JUnit 测试用例中的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3715743/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 03:51:04  来源:igfitidea点击:

How can I compare files in a JUnit test case?

javaunit-testingjunittext-files

提问by Saikios

I want to implement JUnit on a small project I'm working on because I want to learn a little bit about it.

我想在我正在做的一个小项目上实现 JUnit,因为我想了解一些关于它的知识。

The tutorials that I read all make reference to methods that have a particular output.

我阅读的教程都引用了具有特定输出的方法。

In my case my output are files, how can I do this? any simple example? any approach that could help me with this?

在我的情况下,我的输出是文件,我该怎么做?有什么简单的例子吗?任何可以帮助我解决这个问题的方法?

The files are raw text files that are build by a void private method.

这些文件是由 void 私有方法构建的原始文本文件。

采纳答案by Jon Freedman

You want to get a correct output file for a given set of inputs, and setup a test to call your void method with those inputs, and then compare your validated output file against whats produced by your method. You need to make sure that you have some way of specifying where your method will output to, otherwise your test will be very brittle.

您希望为给定的一组输入获得正确的输出文件,并设置一个测试以使用这些输入调用您的 void 方法,然后将您验证的输出文件与您的方法产生的内容进行比较。您需要确保您有某种方式来指定您的方法将输出到哪里,否则您的测试将非常脆弱。

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void testXYZ() {
    final File expected = new File("xyz.txt");
    final File output = folder.newFile("xyz.txt");
    TestClass.xyz(output);
    Assert.assertEquals(FileUtils.readLines(expected), FileUtils.readLines(output));
}

Uses commons-io FileUtilsfor convinience text file comparison & JUnit's TemporaryFolderto ensure the output file never exists before the test runs.

使用 commons-io FileUtils进行方便的文本文件比较和 JUnit 的TemporaryFolder以确保在测试运行之前输出文件永远不存在。

回答by Bozho

After your methods write the file, in the unit-test you can read the file and verify whether it is written correctly.

在您的方法写入文件后,在单元测试中您可以读取文件并验证它是否正确写入。

Another thing that makes sense is to have your methods split in one that retrieves that data and returns it to the methods that merely writes it to a file. Then you can verify whether the data returned by the first method is fine.

另一件有意义的事情是将您的方法拆分为一个,以检索该数据并将其返回给仅将其写入文件的方法。然后就可以验证第一种方法返回的数据是否正常。

And another plausible approach would be to pass an OutputStreamto the method that writes the data. In the "real code" you can pass a FileOutputStream/ FileWriter, while in the test-code you can write a mock implementation of OutputStreamand check what is being written to it.

另一种可行的方法是将 an 传递OutputStream给写入数据的方法。在“真实代码”中,您可以传递FileOutputStream/ FileWriter,而在测试代码中,您可以编写模拟实现OutputStream并检查正在写入的内容。

回答by Amir Rachum

If you can't control the method to put the output in a stream, then I'd say you need to refactor your code so that the method receives a stream in the parameter (or in the constructor of its class).

如果您无法控制将输出放入流中的方法,那么我会说您需要重构代码,以便该方法在参数(或其类的构造函数中)接收流。

After that, testing is pretty easy - you can just check the stream. Easily testable code usually equals good code.

之后,测试非常简单 - 您只需检查流即可。易于测试的代码通常等于好的代码。

回答by Ashley Walton

Although your question may seem simplistic it does strike to the heart of unit testing, one needs to write well formed code that is testable. This is why some experts advise that one should write the unit test first and then the implementing class.

尽管您的问题可能看起来很简单,但它确实触及了单元测试的核心,但需要编写可测试的格式良好的代码。这就是为什么一些专家建议首先编写单元测试,然后编写实现类的原因。

In your case I suggest you allow your method to execute and create the file(s) expected, following which your unit test(s) can analyse that the files are formed correctly.

在您的情况下,我建议您允许您的方法执行并创建预期的文件,然后您的单元测试可以分析文件是否正确形成。

回答by Jarekczek

Use junitx.framework.FileAssertclass from junit-addons project. Other links:

使用junitx.framework.FileAssert从类JUnit插件项目。其他链接:

One of the methods:

方法之一:

assertEquals(java.lang.String message,
             java.io.Reader expected,
             java.io.Reader actual)