单元测试。模拟 FileOutPutStream 和云存储

时间:2021-07-21 11:29:14

标签: junit mocking google-cloud-storage

我是 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));

  }

0 个答案:

没有答案
相关问题