测试通过请求下载 Excel 文件

huangapple go评论77阅读模式
英文:

testing excel-file download by request

问题

MvcRequester.on(mockMvc)
             .to("/api/reports/complaints/full")                                      
             .get()
             .doExpect(status().isOk())
             .returnAs(MultipartFile.class); //drop here, tried to use File, InputStream, FileInputStream

这是测试的一部分,用于向端点发送请求。从该端点返回一个Excel文件。请告诉我如何将响应写入一个变量中。

这里是响应主体和异常

我使用自定义库将文件注入到响应中,它百分百正常工作。

以下是将文件添加到响应的控制器方法的末尾:

@GetMapping("/complaints/full")
@ResponseBody
public void getComplaintsFullReport(SearchComplaintDto dto,
                                    HttpServletResponse servletResponse) {

    SearchComplaintArgument argument = complaintMapper.toSearchArgument(dto);

    File file = buildComplaintsReportAction.execute(argument);
    FileResponse.builder()
                .file(file)
                .filename("Report_"
                                  .concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy_HH.mm")))
                                  .concat(".xlsx"))
                .mimeType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                .response(servletResponse)
                .build();
}

回答问题"如何将响应写入一个变量?"。

英文:
 MvcRequester.on(mockMvc)
             .to("/api/reports/complaints/full")                                        
             .get()
             .doExpect(status().isOk())
             .returnAs(MultipartFile.class); //drop here, tried to use File, InputStream, FileInputStream

I this is the part of the test that makes sending the request to the endpoint. An excel file comes from this endpoint. Please tell me how to write the response into a variable.
here is response body and exception.
I use custom library for inject file into response. It's work correct 100%.

Here is end of controller method with adding file into response

@GetMapping("/complaints/full")
    @ResponseBody
    public void getComplaintsFullReport(SearchComplaintDto dto,
                                        HttpServletResponse servletResponse) {

        SearchComplaintArgument argument = complaintMapper.toSearchArgument(dto);

        File file = buildComplaintsReportAction.execute(argument);
        FileResponse.builder()
                    .file(file)
                    .filename("Report_"
                                      .concat(LocalDateTime.now().format(DateTimeFormatter.ofPattern("dd.MM.yyyy_HH.mm")))
                                      .concat(".xlsx"))
                    .mimeType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
                    .response(servletResponse)
                    .build();
    }

Answer to the question "How can i write the response into a variable?".

答案1

得分: 0

byte[] response = MvcRequester.on(mockMvc)
                              .to("/api/reports/complaints/full")
                              .get()
                              .doExpect(status().isOk())
                              .returnResponse()
                              .getContentAsByteArray();

change dependency to (before i used not this dependency, it was custom dependency with same appointment)

<dependency>
    <groupId>com.jupiter-tools</groupId>
    <artifactId>mvc-requester</artifactId>
    <version>0.4</version>
    <scope>test</scope>
</dependency>

Then i got response as byte array, i can compare it with expected file as i wish.
英文:
        byte[] response = MvcRequester.on(mockMvc)
                                  .to(&quot;/api/reports/complaints/full&quot;)
                                  .get()
                                  .doExpect(status().isOk())
                                  .returnResponse()
                                  .getContentAsByteArray();

change dependency to (before i used not this dependency, it was custom dependency with same appointment)

   &lt;dependency&gt;
        &lt;groupId&gt;com.jupiter-tools&lt;/groupId&gt;
        &lt;artifactId&gt;mvc-requester&lt;/artifactId&gt;
        &lt;version&gt;0.4&lt;/version&gt;
        &lt;scope&gt;test&lt;/scope&gt;
    &lt;/dependency&gt;

Then i got response as byte array, i can compare it with expected file as i wish.

huangapple
  • 本文由 发表于 2023年7月12日 22:28:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76671676.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定