4

I'm currently asking you a question because I'm using a spring to write a test code that I don't know how to solve.

The problem i'm trying to solve now is that i don't know how to send DTO to send Form Post Function using BodyInserters.

The codes below are my DTO and test codes.

StudentInfo studentInfo = StudentInfo.builder()
                .studentId(2014l)
                .name("s1")
                .email("a@b.c")
                .phoneNumber("1234")
                .universityCode("cbnu")
                .build();

ContestInfo contestInfo = ContestInfo.builder()
                .name("contest 1")
                .description("easy contest")
                .activeTime(LocalDateTime.now())
                .inActiveTime(LocalDateTime.now())
                .startTime(LocalDateTime.now())
                .endTime(LocalDateTime.now())
                .freezeTime(LocalDateTime.now())
                .unFreezeTime(LocalDateTime.now())
                .build();

WebTestClient.ResponseSpec responseSpec = webTestClient.post()
                .uri("/questions")
                .contentType(MediaType.MULTIPART_FORM_DATA)
                .body(BodyInserters.fromFormData("contest", AcceptanceTestUtils.ConvertObjectToMap(contestInfo).toString())
                        .with("student", AcceptanceTestUtils.ConvertObjectToMap(studentInfo).toString())
                        .with("activeTime", String.valueOf(LocalDateTime.now()))
                        .with("problemNumber", String.valueOf(1L))
                        .with("content", "test")
                        .with("response", " ")
                        .with("createTime", String.valueOf(LocalDateTime.now())))
                .exchange()
                .expectStatus()
                .isCreated()
                .expectHeader().valueMatches("location", "/questions/[1-9]+[0-9]*");

Especially among the parts of the test code.

.body ("contest", AcceptanceTestUtils.ConvertObjectToMap").toString()

.with ("student", AcceptanceTestUtils.ConvertObjectToMap).toString()

These are the two lines that I've been thinking about.

I thought I could convert the DTO into a map and deliver the string again, but there is a problem that keeps not being delivered.

contest=%7BactiveTime%3D2020-05-20T12%3A31%3A01.718%2C+name%3Dcontest+1%2C+description%3Deasy+contest%2C+startTime%3D2020-05-20T12%3A31%3A01.719%2C+freezeTime%3D2020-05-20T12%3A31%3A01.719%2C+endTime%3D2020-05-20T12%3A31%3A01.719%2C+unFreezeTime%3D2020-05-20T12%3A31%3A01.719%2C+inActiveTime%3D2020-05-20T12%3A31%3A01.719%7D&student=%7BstudentId%3D2014%2C+phoneNumber%3D1234%2C+name%3Ds1%2C+universityCode%3Dcbnu%2C+email%3Da%40b.c%7D&activeTime=2020-05-20T12%3A31%3A01.749&problemNumber=1&content=test&response=+&createTime=2020-05-20T12%3A31%3A01.749

The values transferred are as above. How can I fix this?

Liquid.Bear
  • 333
  • 6
  • 21

0 Answers0