Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何从模拟路径加载模拟文件_Java_Spring Boot_Unit Testing_Junit_Mockito - Fatal编程技术网

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

Java 如何从模拟路径加载模拟文件,java,spring-boot,unit-testing,junit,mockito,Java,Spring Boot,Unit Testing,Junit,Mockito,我正在尝试为一个函数编写单元测试,该函数尝试从磁盘获取文件并对其进行流式传输: public InputStream downloadFile(String folderName, String fileName) throws FileNotFoundException { File name = Paths.get("/tmp/portal", folderName, fileName).toFile(); return new FileInputStr

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

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不存在


您收到一个错误
testFile not exists
,因为
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);
    }
}

感谢我写下返回值是这样的,new
CipherInputStream(newFileInputStream(file),service.getDecryptCipher())由于您的执行,我收到一个错误,文件无法删除,如何模拟密码?你能相应地更改代码吗?然后我将接受答案:)@Catalina我已使用模拟密码更新了答案。我收到以下错误:
警告:关闭java.io时出错。BufferedReader@706507f8java.lang.IllegalStateException:密码未初始化
也有此错误:
ed:java.nio.file.FileSystemException:C:\Users\catalina\AppData\Local\Temp\junit10056578990060702957\testFile:该进程无法访问该文件,因为它正被另一进程使用。
请帮助我:(