Spring MVC 测试excel-按请求下载文件

daolsyd0  于 10个月前  发布在  Spring
关注(0)|答案(1)|浏览(68)
MvcRequester.on(mockMvc)
             .to("/api/reports/complaints/full")                                        
             .get()
             .doExpect(status().isOk())
             .returnAs(MultipartFile.class); //drop here, tried to use File, InputStream, FileInputStream

字符串
这是测试的一部分,用于将请求发送到端点。excel文件来自此端点。请告诉我如何将响应写入变量。
here is response body and exception的数据。
我使用自定义库注入文件到响应。工作正确率100%。
这里是控制器方法的结尾,将文件添加到响应中

@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();
    }


如何将响应写入变量?“.

xwbd5t1u

xwbd5t1u1#

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

字符串
将依赖项更改为(之前我使用的不是此依赖项,它是具有相同约会的自定义依赖项)

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


然后我得到了字节数组的响应,我可以按照我的意愿将其与预期的文件进行比较。

相关问题