【问题标题】: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 不存在

【问题讨论】:

    标签: java spring-boot unit-testing junit mockito


    【解决方案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.
    • 请帮帮我:(
    猜你喜欢
    • 1970-01-01
    • 2011-08-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多