我在春季启动时使用MockMvc multipart来测试一个接受多部分文件的RestController。
当我没有在application.yml中设置context-path时,测试运行良好,但是当我将其设置为某个值时,mockMvc返回404。
以下配置有效:
Application.yml
server:
port: 8080
servlet:
context-path: /
use-forward-headers: true
forward-headers-strategy: FRAMEWORK
Test.java
// setup
String templateUploadUrl = "http://localhost:8080/v1/file/upload?name=%s";
byte[] templateContent = DataFactory.getFileAsByteArray();
String contentParameterName = "file";
String fileName = "fileName";
MockMultipartFile multipartFile = new MockMultipartFile(contentParameterName, templateContent);
templateUploadUrl = String.format(templateUploadUrl, fileName);
this.mvc.perform(multipart(templateUploadUrl).file(multipartFile));
但是以下配置给出了404:
Application.yml
server:
port: 8080
servlet:
context-path: /context
use-forward-headers: true
forward-headers-strategy: FRAMEWORK
Test.java
// setup
String templateUploadUrl = "http://localhost:8080/context/v1/file/upload?name=%s";
byte[] templateContent = DataFactory.getFileAsByteArray();
String contentParameterName = "file";
String fileName = "fileName";
MockMultipartFile multipartFile = new MockMultipartFile(contentParameterName, templateContent);
templateUploadUrl = String.format(templateUploadUrl, fileName);
this.mvc.perform(multipart(templateUploadUrl).file(multipartFile));