- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个简单的 spring 应用程序,带有一个需要多部分文件的 Controller 。出于测试目的,我设置了 MaxUploadSize
大小仅为 50 字节。当我在 tomcat 中部署 war 时,我得到了预期的 MaxUploadSizeExceededException
对于较大的文件,但如果我在单元测试中使用相同的文件,则测试不起作用。
配置
@Configuration
@EnableWebMvc
@ComponentScan(basePackageClasses = {...})
public class MyApplication extends WebMvcConfigurerAdapter {
@Bean
public CommonsMultipartResolver multipartResolver() {
CommonsMultipartResolver commonsMultipartResolver =
new CommonsMultipartResolver();
commonsMultipartResolver.setDefaultEncoding("utf-8");
commonsMultipartResolver.setMaxUploadSize(50);
return commonsMultipartResolver;
}
}
@RestController
@RequestMapping("/metadata")
public class MyController {
@RequestMapping(method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
produces = MediaType.APPLICATION_JSON_VALUE)
public String metadata(@RequestParam MultipartFile file) {
//some processing...
}
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = MyApplication.class)
public class MyControllerTest {
...
@Test
public void testDocx() throws Exception {
byte[] docx = FileUtils.readFileToByteArray(new File(
"src/test/resources/testdocs/test.docx"));
MockMultipartFile multipartFile = new MockMultipartFile("file", "test.docx", "", docx);
this.mvc.perform(fileUpload("/metadata").file(multipartFile))
.andDo(print()).andExpect(status().is5xxServerError());
//doesn't work, returns 200
}
}
最佳答案
我遇到了同样的问题,幸运的是我能够解决它。
我假设 this.mvc
是 MockMvc
的一个实例.据此 spring文档:
Another useful approach is to not start the server at all but to test only the layer below that, where Spring handles the incoming HTTP request and hands it off to your controller. That way, almost of the full stack is used, and your code will be called in exactly the same way as if it were processing a real HTTP request but without the cost of starting the server. To do that, use Spring’s MockMvc and ask for that to be injected for you by using the @AutoConfigureMockMvc annotation on the test case.
关于java - 测试中未抛出 MaxUploadSizeExceededException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46097516/
我有一个简单的 spring 应用程序,带有一个需要多部分文件的 Controller 。出于测试目的,我设置了 MaxUploadSize大小仅为 50 字节。当我在 tomcat 中部署 war
MaxUploadSizeExceededException 当我上传的文件大小超过允许的最大值时出现异常。我想在出现此异常时显示错误消息(如验证错误消息)。在 Spring 3 中如何处理此异常以执
我正在开发图像上传系统。我使用 CommonsMultipartResolver 设置 maxUploadSize。当我尝试上传超过最大尺寸的图像文件时,会发生 MaxUploadSizeExcced
所以我使用 Spring Webflow 上传一个文件和带有最大文件大小参数的 CommonsMultipartResolver。 当最大文件大小超过 CommonsMultipartResolver
我想检查上传文件的大小并阻止文件完全加载到内存中。我正在使用 CommonsMultipartFile。上传的文件将被处理并保存在数据库中。 AbstractCoupleUploadControlle
我有一个包含三个文本字段和一个文件上传字段的表单。当我遇到 MaxUploadSizeExceededException 异常时,我可以使用实现 HandlerExceptionResolver 的类
我使用的是 Spring 3.2.0。根据this答案,我在带注释的 Controller 中有相同的方法,它实现了 HandlerExceptionResolver 接口(interface),例如
我正在尝试处理最大上传大小异常,但我发现的所有解决方案都失败了,我尝试实现 HandlerExceptionResolver、@ControllerAdvice 和由 @ExceptionHandle
我有简单的 SpringBoot 应用程序文件上传功能,其中最大文件上传文件大小为 2 MB。 我已经配置了 multipart.max-file-size=2MB 它工作正常。但是当我尝试上传大于
我发现 Apache Tomcat 允许以下配置,采用硬编码或注释方法。我不确定最大文件大小是在上传过程中还是在文件上传到临时位置之后计算的。该文档指示以下内容: The @MultipartConf
我正在使用 Spring Web 4.0.5、Spring Security 3.2.4、Commons FileUpload 1.3.1、Tomcat 7,我得到了一个丑陋的 MaxUploadSi
当超过文件大小时,如何通过文件上传拦截和发送自定义错误消息。我在 Controller 类中有一个带注释的异常处理程序,但请求没有到达 Controller 。我在这个链接上找到的答案 How to
我对此进行了一些研究,结果相互矛盾。要处理此错误,有人说我需要在我的一个 Controller 中实现 HandlerExceptionResolver。 这里有一些链接: How to handle
我有这段代码(就像在 Spring 的引用中): 当用户尝试上传超过 100 kB 的文件时,将显示 HTTP 状态为 500 的服务器错误页面和堆栈跟踪。如何
我无法捕捉和优雅地处理公共(public)文件上传的 FileUploadBase.SizeLimitExceededException或 Spring 的MaxUploadSizeExceededE
我是一名优秀的程序员,十分优秀!