如何将临时文件夹传递给以文件作为参数的方法(JUNIT)
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");
//Here I get an error because the parameter should be of type File and not TemporaryFolder
listFilesService.processFiles (temporaryFolder);
}
你能帮我正确编码吗
非常感谢
您应该使用 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");
// Pass the root of the TemporaryFolder which is of type File
processFiles(temporaryFolder.getRoot());
}
这可以通过在 processFiles
的 for
循环中添加一个 System.out.println(file.getName());
来验证是否有效。以上测试的输出为:
tempFile.txt
tempFile2.txt
我有一个将 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");
//Here I get an error because the parameter should be of type File and not TemporaryFolder
listFilesService.processFiles (temporaryFolder);
}
你能帮我正确编码吗 非常感谢
您应该使用 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");
// Pass the root of the TemporaryFolder which is of type File
processFiles(temporaryFolder.getRoot());
}
这可以通过在 processFiles
的 for
循环中添加一个 System.out.println(file.getName());
来验证是否有效。以上测试的输出为:
tempFile.txt
tempFile2.txt