英文:
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<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();
}
}
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("tempFile.txt");
final File tempFile2 = temporaryFolder.newFile("tempFile.txt");
FileUtils.writeStringToFile(tempFile, "content1", "ISO-8859-1");
FileUtils.writeStringToFile(tempFile2, "content2", "ISO-8859-1");
//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());
}
可以通过在 processFiles
的 for
循环内添加 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("tempFile.txt");
final File tempFile2 = temporaryFolder.newFile("tempFile2.txt");
FileUtils.writeStringToFile(tempFile, "content1", "ISO-8859-1");
FileUtils.writeStringToFile(tempFile2, "content2", "ISO-8859-1");
// 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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论