how to unit test a reactive spring api adding a list of strings as body request?

i have this url which is working:

enter image description here

which returns only the database missing elements of the input. i have a third party not modifiable service method like this:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Mono<List<String>> getInvalidList(final Flux<String> input)
</code>
<code>public Mono<List<String>> getInvalidList(final Flux<String> input) </code>
public Mono<List<String>> getInvalidList(final Flux<String> input)

and to make this api work i’ve wrote this handler method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) {
final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req);
return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
.body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class)
.doOnSuccess(result -> log.traceExit(message, result));
}
</code>
<code>public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) { final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req); return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON) .body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class) .doOnSuccess(result -> log.traceExit(message, result)); } </code>
public Mono<ServerResponse> getInvalidKeyItems(final ServerRequest req) {
    final EntryMessage message = log.traceEntry("getInvalidKeyItems[request={}]", req);

    return ServerResponse.ok().contentType(MediaType.APPLICATION_JSON)
            .body(service.getInvalidList(req.bodyToFlux(Integer.class).map(Object::toString)), List.class)
            .doOnSuccess(result -> log.traceExit(message, result));
}

and this router method:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code> @Bean
public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) {
return route(POST("/somePath").and(POST_AND_ACCEPT_JSON),
handler::getInvalidKeyItems);
}
</code>
<code> @Bean public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) { return route(POST("/somePath").and(POST_AND_ACCEPT_JSON), handler::getInvalidKeyItems); } </code>
    @Bean
    public RouterFunction<ServerResponse> getInvalidKeyItems(final KeyItemHandler handler) {
        return route(POST("/somePath").and(POST_AND_ACCEPT_JSON),
                handler::getInvalidKeyItems);
    }

which POST_AND_ACCEPT_JSON is a custom short variable for

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON}));
</code>
<code>RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON})); </code>
RequestPredicates.method(HttpMethod.POST).and(RequestPredicates.accept(new MediaType[]{MediaType.APPLICATION_JSON}));

again, all of this is already working. what is not working is my router unit test:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>@SpringBootTest
@ExtendWith({SpringExtension.class})
@AutoConfigureWebTestClient
class KeyItemRouterTest {
@Autowired
WebTestClient client;
@MockBean
KeyItemService service;
@Test
@WithMockUser
void validateKeyItemTest() {
Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3"))))
.thenReturn(Mono.just(List.of("1")));
client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON)
.body(Mono.just(List.of("1", "2", "3")), List.class)
.exchange()
.expectStatus().isOk()
.expectBody(List.class)
.value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list));
}
</code>
<code>@SpringBootTest @ExtendWith({SpringExtension.class}) @AutoConfigureWebTestClient class KeyItemRouterTest { @Autowired WebTestClient client; @MockBean KeyItemService service; @Test @WithMockUser void validateKeyItemTest() { Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3")))) .thenReturn(Mono.just(List.of("1"))); client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON) .body(Mono.just(List.of("1", "2", "3")), List.class) .exchange() .expectStatus().isOk() .expectBody(List.class) .value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list)); } </code>
@SpringBootTest
@ExtendWith({SpringExtension.class})
@AutoConfigureWebTestClient
class KeyItemRouterTest {

    @Autowired
    WebTestClient client;
    @MockBean
    KeyItemService service;

    @Test
    @WithMockUser
    void validateKeyItemTest() {
        Mockito.when(service.getInvalidList(Flux.fromIterable(List.of("1", "2", "3"))))
                .thenReturn(Mono.just(List.of("1")));

        client.post().uri("/somePath").contentType(MediaType.APPLICATION_JSON)
                .body(Mono.just(List.of("1", "2", "3")), List.class)

                .exchange()
                .expectStatus().isOk()

                .expectBody(List.class)
                .value(list -> Assertions.assertIterableEquals(List.of("2", "3"), list));
    }

which gets me this error:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"}
java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST>
Expected :200 OK
Actual :400 BAD_REQUEST
</code>
<code>{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"} java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST> Expected :200 OK Actual :400 BAD_REQUEST </code>
{"timestamp":1714607064355,"path":"/somePath","status":"BAD_REQUEST","error":"'producer' must not be null","message":"'producer' must not be null","requestId":"46a88fc7","exception":"java.lang.IllegalArgumentException"}


java.lang.AssertionError: Status expected:<200 OK> but was:<400 BAD_REQUEST>
Expected :200 OK
Actual   :400 BAD_REQUEST

i’ve tried changing

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.body(Mono.just(List.of("1", "2", "3")), List.class)
</code>
<code>.body(Mono.just(List.of("1", "2", "3")), List.class) </code>
.body(Mono.just(List.of("1", "2", "3")), List.class)

to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.bodyValue(List.of("1", "2", "3"))`
</code>
<code>.bodyValue(List.of("1", "2", "3"))` </code>
.bodyValue(List.of("1", "2", "3"))`

to

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class))
</code>
<code>.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class)) </code>
.body(BodyInserters.fromPublisher(Mono.just(List.of("1","2","3")), List.class))

but all of them resulted in the same error. how should i add a list of strings in my body request when unit testing?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật