作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个单元测试,在使用 Spring 4.3 转换方法以返回 StreamingResponseBody 之后,我试图检查发出的异步请求的响应。
测试方法如下:
final MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
MvcResult mvcResult1 = mockMvc.perform(
get("/reports/generic/100?FIELD1=&FIELD3=").headers(STANDARD_HEADERS.get()))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();
mvcResult1.getAsyncResult();
mockMvc.perform(asyncDispatch(mvcResult1))
.andExpect(status().isOk())
.andExpect(content().contentType("text/csv"))
.andExpect(content().string("Test Data" + System.lineSeparator() + "FIELD1=" + System.lineSeparator() + "FIELD3=" + System.lineSeparator()))
它调用的方法如下所示:
public StreamingResponseBody streamReport(@PathVariable("type") @NotNull String type, @PathVariable("id") @NotNull Long id, ReportConfiguration config, HttpServletResponse response) throws Exception {
ReportServiceHandler handler = reportHandlerFactory.getHandler(type);
final String reportFilename = handler.getReportFileName(id, reportConfiguration);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + reportFilename);
response.setContentType("text/csv");
return new StreamingResponseBody() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
try {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + reportFilename);
response.setContentType("text/csv");
ServletOutputStream out = (ServletOutputStream) outputStream;
handler.generateReport(out, id, reportConfiguration);
out.flush();
} catch ( Exception e ) {
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "inline");
response.setContentType("");
throw new IOException(e);
}
}
};
}
调试显示原始请求中包含来自异步的响应,但异步响应对象(在 mvcResult1 中)未在 期间被复制asyncDispatch 因此 contextType 和内容字符串都为 null。
这里是否遗漏了处理异步 mvcResult 的测试配置,以便可以断言内容?
最佳答案
我在这方面做得相当成功。这是我的不同做法:
return new StreamingResponseBody() {
@Override
public void writeTo(OutputStream outputStream) throws IOException {
try {
... same
out.flush();
} catch ( Exception e ) {
... your error handling
} finally {
// Try to close the stream
try {
if (out != null) {
out.close();
}
} catch (Throwable t) {
// not much we can do
}
}
}
};
现在,当我的测试与您的测试平行时,看起来是这样的。阅读您的代码时,我不确定您为什么调用 perform() 两次。对我来说只需要一次。
//Same
final MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
// Same
MvcResult mvcResult1 = mockMvc.perform(
get(....))
.andExpect(status().isOk())
.andExpect(request().asyncStarted())
.andReturn();
mvcResult1.getAsyncResult();
// From this point on, I have complete response,
// if it's not bugging you to place the entire file in memory
String thisIsYourFileContent = mvcResult1.getResponse().getContentAsString();
// Go on asserting the content of the file...
希望这对某人有所帮助。
关于java - MockMvcRequestBuilders.asyncDispatch 提供一个空的 HTTP Response 和 contentType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54412062/
我有一个单元测试,在使用 Spring 4.3 转换方法以返回 StreamingResponseBody 之后,我试图检查发出的异步请求的响应。 测试方法如下: final MockMvc mock
我有一个返回异步结果的 Spring Boot 2 MVC 应用程序。请注意,虽然我使用的是 Mono,但它不是 webflux 应用程序,而是 webmvc,因此结果将被视为 Spring MVC
我是一名优秀的程序员,十分优秀!