MockMVC分段返回带有上下文路径的404

时间:2020-08-09 18:39:59

标签: spring spring-boot spring-mvc

我在春季启动时使用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));

0 个答案:

没有答案