我是 JUnit 测试的新手。我正在尝试模拟以下功能。模拟时面临异常( fileOutputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE); )。请指导我模拟这个功能。
public ResponseEntity<Resource> downloadFile(String fileName) throws IOException {
Blob blob = storage.get(tutorialBucket, fileName);
ReadChannel readChannel = blob.reader();
File file = new File("/tmp/" + fileName);
file.getParentFile().mkdirs();
FileOutputStream fileOutputStream = new FileOutputStream(file);
fileOutputStream.getChannel().transferFrom(readChannel, 0, Long.MAX_VALUE);
fileOutputStream.close();
FileInputStream inputStream = new FileInputStream(file);
HttpHeaders headers = new HttpHeaders();
headers.set("Content-Disposition", "attachment; filename=" + fileName);
return ResponseEntity
.ok()
.headers(headers)
.contentLength(file.length())
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(new InputStreamResource(inputStream));
}