如何将临时文件夹传递给一个接受File参数的方法(JUNIT)?

huangapple go评论141阅读模式
英文:

How can I pass a temporary folder to a method that take a File as parameter (JUNIT)

问题

我有一个以File作为参数的方法,我想使用@Rules temporaryFolder来进行JUnit测试。

这是我的方法:

public void processFiles(final File folder)
{
    
    File[] fileNames = folder.listFiles();
    List<String> lines = new ArrayList<>();
    boolean isFirstFile = true;
    try
    {
        for (File file : fileNames)
        {
                List<String> list = readContent(file);
                list.forEach(i -> lines.add(i));
                 
            }
    }catch (IOException e)
    {
        e.printStackTrace();
    }
}

这是我想要进行测试的方式:

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void test() throws IOException {
    
    final File tempFile = temporaryFolder.newFile("tempFile.txt");
    final File tempFile2 = temporaryFolder.newFile("tempFile.txt");
 

    FileUtils.writeStringToFile(tempFile, "content1", "ISO-8859-1");
    
    FileUtils.writeStringToFile(tempFile2, "content2", "ISO-8859-1");
    
    
     //在这里出现错误,因为参数应该是File类型而不是TemporaryFolder
    listFilesService.processFiles (temporaryFolder);
  
}

你想要的正确编码方式是:

@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void test() throws IOException {
    
    final File tempFolder = folder.newFolder("tempFolder");
    final File tempFile1 = new File(tempFolder, "tempFile1.txt");
    final File tempFile2 = new File(tempFolder, "tempFile2.txt");

    FileUtils.writeStringToFile(tempFile1, "content1", "ISO-8859-1");
    FileUtils.writeStringToFile(tempFile2, "content2", "ISO-8859-1");

    listFilesService.processFiles(tempFolder);
}

这样你的测试代码会使用TemporaryFolder来创建一个临时文件夹和文件,并将临时文件夹作为参数传递给listFilesService.processFiles方法,以便进行测试。

英文:

I have a method that takes File as a parameter, I want to junit test it using the @Rules temporaryFolder

Here is my method:

    public void processFiles(final File folder)
    {
        
        File[] fileNames = folder.listFiles();
        List&lt;String&gt; lines = new ArrayList&lt;&gt;();
        boolean isFirstFile = true;
        try
        {
            for (File file : fileNames)
            {
                    List&lt;String&gt; list = readContent(file);
                    list.forEach(i -&gt; lines.add(i));
                     
                }
        }catch (IOException e)
        {
            e.printStackTrace();
        }
    }

And here is how I want to test it:

 @Rule
 public TemporaryFolder folder = new TemporaryFolder();

 @Test
 public void test() throws IOException {
        
        final File tempFile = temporaryFolder.newFile(&quot;tempFile.txt&quot;);
        final File tempFile2 = temporaryFolder.newFile(&quot;tempFile.txt&quot;);
     

        FileUtils.writeStringToFile(tempFile, &quot;content1&quot;, &quot;ISO-8859-1&quot;);
        
        FileUtils.writeStringToFile(tempFile2, &quot;content2&quot;, &quot;ISO-8859-1&quot;);
        
        
         //Here I get an error because the parameter should be of type File and not TemporaryFolder 
        listFilesService.processFiles (temporaryFolder);
      
    }

Can you help me to how I should code this properly
Thanks a lot in advance

答案1

得分: 1

你应该使用 TemporaryFolder.getRoot() 方法。然后,你的测试应该是:

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void test() throws IOException {

    final File tempFile = temporaryFolder.newFile("tempFile.txt");
    final File tempFile2 = temporaryFolder.newFile("tempFile2.txt");


    FileUtils.writeStringToFile(tempFile, "content1", "ISO-8859-1");

    FileUtils.writeStringToFile(tempFile2, "content2", "ISO-8859-1");


    // 传递 TemporaryFolder 的根目录,它的类型是 File
    processFiles(temporaryFolder.getRoot());

}

可以通过在 processFilesfor 循环内添加 System.out.println(file.getName()); 来验证它的工作方式。上面测试的输出为:

tempFile.txt
tempFile2.txt
英文:

You should be using the TemporaryFolder.getRoot() method. Then, your test would be:

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Test
public void test() throws IOException {

    final File tempFile = temporaryFolder.newFile(&quot;tempFile.txt&quot;);
    final File tempFile2 = temporaryFolder.newFile(&quot;tempFile2.txt&quot;);


    FileUtils.writeStringToFile(tempFile, &quot;content1&quot;, &quot;ISO-8859-1&quot;);

    FileUtils.writeStringToFile(tempFile2, &quot;content2&quot;, &quot;ISO-8859-1&quot;);


    // Pass the root of the TemporaryFolder which is of type File
    processFiles(temporaryFolder.getRoot());

}

This can be verified as working by adding a System.out.println(file.getName()); within the for loop of processFiles. The output of the above test is:

tempFile.txt
tempFile2.txt

</details>



huangapple
  • 本文由 发表于 2020年8月4日 02:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/63235328.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定