如何从模拟路径加载模拟文件

Posted

技术标签:

【中文标题】如何从模拟路径加载模拟文件【英文标题】:How to load a mock file from mock path 【发布时间】:2021-02-19 01:03:38 【问题描述】:

我正在尝试为一个尝试从磁盘获取文件并将其流式传输的函数编写单元测试:

public InputStream downloadFile(String folderName, String fileName) throws FileNotFoundException 
    File name = Paths.get("/tmp/portal", folderName, fileName).toFile();

    return new FileInputStream(name);
  

我的尝试代码

 @InjectMock MyService myService;
   
    @TempDir
        Path mockDirectory;
     @Test
        void downloadFile() throws IOException 
     Path mockFile = mockDirectory.resolve("testFile");
            Files.createFile(mockFile);
     String folderName= mockFile.subpath(5, 6).toString();
     String filename= mockFile.getFileName().toString();
        InputStream inputStreamResult = myService.downloadFile(folderName, filename);
    

错误是

文件:testFile 不存在

【问题讨论】:

【参考方案1】:

您收到错误testFile dont exsits,因为在downloadFile 方法中有一个硬编码路径"/tmp/portal"

以下是下载文件的单元测试示例:

interface Service 
    Cipher getDecryptCipher();


class FileDownloader 

    private final Service service;

    public FileDownloader(Service service) 
        this.service = service;
    

    public InputStream downloadFile(String folderName, String fileName) throws FileNotFoundException 
        File file = Paths.get(folderName, fileName).toFile();
        return new CipherInputStream(new FileInputStream(file), service.getDecryptCipher());
    


@ExtendWith(MockitoExtension.class)
class FileDownloaderTest 

    @TempDir
    Path directory;
    @Mock
    Service service;
    @Mock
    Cipher cipher;
    @InjectMocks
    FileDownloader fileDownloader;

    @Test
    void fileContentIsDownloaded() throws IOException 

        String testFileName = "testFile";
        String testFileContent = "test text";

        Path testFile = directory.resolve(testFileName);
        Files.createFile(testFile);
        Files.write(testFile, testFileContent.getBytes());
        when(service.getDecryptCipher()).thenReturn(cipher);

        InputStream actualInputStream = fileDownloader.downloadFile(directory.toString(), testFileName);

        CipherInputStream expectedInputStream = new CipherInputStream(new ByteArrayInputStream(testFileContent.getBytes()), cipher);
        assertThat(actualInputStream).hasSameContentAs(expectedInputStream);
    

【讨论】:

谢谢我真的忘记写返回值是这样的,新的CipherInputStream(new FileInputStream(file), service.getDecryptCipher()); 与你的实施我得到了文件无法删除的错误,如何模拟密码?你能相应地更改代码吗?然后我会接受答案:) @Catalina 我已经用模拟密码更新了答案。 我收到此错误:WARNING: Error occurred while closing java.io.BufferedReader@706507f8 java.lang.IllegalStateException: Cipher not initialized 也有这个错误:ed: java.nio.file.FileSystemException: C:\Users\catalina\AppData\Local\Temp\junit10056578990060702957\testFile: The process cannot access the file because it is being used by another process. 请帮帮我:(

以上是关于如何从模拟路径加载模拟文件的主要内容,如果未能解决你的问题,请参考以下文章

UIWebView 从本地文件系统加载 HTML 文件

如何从源代码构建 grub2 引导加载程序并使用 qemu 模拟器对其进行测试

Windows 应用程序中的模拟

从框架加载 .xib 文件

加载用户图像适用于模拟器,但不适用于 iphone xcode 6 Swift

使用 pandas 或 numpy 从一个 csv 加载多个数据帧