Spring MVC的测试 - 需要多POST JSON originalFilename(Spri

2019-11-05 02:58发布

我用弹簧启动2.0.3.RELEASE与junit5。 我刚才想测试与mockMvc多的要求。

MockMultipartFile file = new MockMultipartFile("file", "contract.pdf", MediaType.APPLICATION_PDF_VALUE, "<<pdf data>>".getBytes(StandardCharsets.UTF_8));
MockMultipartFile metadata = new MockMultipartFile("metadata", "", MediaType.APPLICATION_JSON_VALUE, objectMapper.writeValueAsString(metadata).getBytes(StandardCharsets.UTF_8));
this.mockMvc.perform(multipart("/documents")
        .file(file)
        .file(metadata))
        .andExpect(status().isNoContent());

但它不与部分元数据(JSON)工作。

我总是异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: originalFilename is required.

怎么了?

编辑:

我的实现是Spring集成DSL。 我认为,异常在调用org.springframework.integration.http.multipart.UploadedMultipartFile 。 如何测试与mockMvc多要求?

@Bean
public IntegrationFlow multipartForm() {
    return IntegrationFlows.from(Http.inboundChannelAdapter("/documents")
            .statusCodeExpression(HttpStatus.NO_CONTENT.toString())
            .requestMapping(m -> m
                    .methods(HttpMethod.POST)
                    .consumes(MediaType.MULTIPART_FORM_DATA_VALUE)
            ))
            .handle(new MultipartReceiver())
            .get();
}

我的示范项目是在GitHub上https://github.com/bulalak/demo-spring-integration-http

Answer 1:

所以我写在这里与我的一个例子您还没有共享您的控制器。

这里是春天的引导与MultipartFile测试:

@RunWith(SpringRunner.class)
@WebMvcTest(UserController.class)
public class UserControllerTest {
    @Autowired
    MockMvc mockMvc;

    @MockBean
    UserService userService;

    @Test
    public void test() throws Exception {
        UserCreateRequest request = new UserCreateRequest();
        request.setName("test");
        request.setBirthDate(new Date());
        request.setGender("male");

        MockMultipartFile file =
                new MockMultipartFile(
                        "file",
                        "contract.pdf",
                        MediaType.APPLICATION_PDF_VALUE,
                        "<<pdf data>>".getBytes(StandardCharsets.UTF_8));

        ObjectMapper objectMapper = new ObjectMapper();

        MockMultipartFile metadata =
                new MockMultipartFile(
                        "request",
                        "request",
                        MediaType.APPLICATION_JSON_VALUE,
                        objectMapper.writeValueAsString(request).getBytes(StandardCharsets.UTF_8));

        mockMvc.perform(
                multipart("/users/")
                        .file(file)
                        .file(metadata)
                        .accept(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk());

    }
}

用户控制器:

@RestController
@RequestMapping("/users")
public class UserController {

    UserService userService;

    @Autowired
    public UserController(UserService userService) {
        this.userService = userService;
    }

    @PostMapping({"/", ""})
    public ResponseEntity<User> post(@RequestPart("request") UserCreateRequest request, @RequestPart("file") MultipartFile file) throws IOException {

        String photoPath = UUID.randomUUID() + file.getOriginalFilename().replaceAll(" ", "").trim();

        // other logic

        request.setPhone(photoPath);

        return ResponseEntity.ok(userService.create(request));
    }
}

UserCreateRequest:

@Data // from lombok
public class UserCreateRequest {
    private String name;
    private String phone;
    private String surname;
    private String gender;
    private Date birthDate;
    private String bio;
    private String photo;
}


文章来源: Spring MVC Test - multipart POST json originalFilename is required