英文:
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("/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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论