上传REST端点测试

时间:2018-10-30 09:11:52

标签: spring-boot spring-webflux jmockit

卡住了测试REST端点,以便使用JMockit和spring boot webflux上传图像。

我的REST端点如下:

    @RestController
    @RequestMapping("/v1/files")
    @RequiredArgsConstructor
    public class FileController {

        private final SomeService someService;

        @PostMapping(consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
        @ResponseStatus(HttpStatus.CREATED)
        public Mono<SomeDTO> upload(@Valid File metadata, Mono<FilePart> file) {
            return file.flatMap(part -> someService.upload(metadata, part));
        }
    }

文件对象仅包含几个必须在请求中填充的字符串字段。

测试上传端点:

@RunWith(SpringRunner.class)
@WebFluxTest(FileController.class)
public class FileControllerTest {

    @Mocked
    private SomeService someService;

    @Autowired
    private WebTestClient webTestClient;

    @Test
    public void testUpload(@Injectable FilePart filePart) {
        File file = new File().setSomeStringField(...).setSomeStringField(...);

        MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
        body.add("file", filePart);
        body.add("someString1", "string1");
        body.add("someString2", "string2");

        new Expectations() {{
            someService.upload(file, filePart);
            result = Mono.just(new SomeDTO(FILE_ID));
        }};

        this.webTestClient.post().uri("/v1/files")
                          .contentType(MediaType.MULTIPART_FORM_DATA)
                          .syncBody(BodyInserters.fromMultipartData(body))
                          .exchange()
                          .expectStatus().isCreated()
                          .expectBody(ResponseEntity.class);

    }
}

现在,测试失败并显示错误:

Content type 'multipart/form-data' not supported
org.springframework.web.reactive.function.UnsupportedMediaTypeException: Content type 'multipart/form-data' not supported

将我的测试更改为以下视图:

@Test
        public void testUpload(@Injectable FilePart filePart) {
            File file = new File().setSomeStringField(...).setSomeStringField(...);

            MultipartBodyBuilder builder = new MultipartBodyBuilder();
            body.asyncPart("file", Mono.just(filePart), FilePart.class);
            body.part("someString1", "string1");
            body.part("someString2", "string2");

            new Expectations() {{
                someService.upload(file, filePart);
                result = Mono.just(new SomeDTO(FILE_ID));
            }};

            this.webTestClient.post().uri("/v1/files")
                      .body(BodyInserters.fromMultipartData(body))
                      .exchange()
                      .expectStatus().isCreated()
                      .expectBody(ResponseEntity.class);

        }

仍然失败,但又出现另一个错误:

Status expected:<201> but was:<500>

> POST http://localhost:8080/v1/files
> WebTestClient-Request-Id: [1]
> Content-Type: [multipart/form-data;boundary=3C1UP7-37twv--Qlh12H5hoTPVN63HLh;charset=UTF-8]

I/O failure: org.springframework.core.codec.CodecException: No suitable writer found for part: file

0 个答案:

没有答案