rest - How to mock Spring WebClient in Unit Test -
we wrote small spring boot rest application, performs rest request on rest endpoint.
@requestmapping("/api/v1") @springbootapplication @restcontroller @slf4j public class application { @autowired private webclient webclient; @requestmapping(value = "/zyx", method = post) @responsebody xyzapiresponse zyx(@requestbody xyzapirequest request, @requestheader httpheaders headers) { webclient.post() .uri("/api/v1/someapi") .accept(mediatype.application_json) .contenttype(mediatype.application_json) .body(bodyinserters.fromobject(request.getdata())) .exchange() .subscribeon(schedulers.elastic()) .flatmap(response -> response.bodytomono(xyzserviceresponse.class).map(r -> { if (r != null) { r.setstatus(response.statuscode().value()); } if (!response.statuscode().is2xxsuccessful()) { throw new processresponseexception( "bad status response code " + response.statuscode() + "!"); } return r; })) .subscribe(body -> { // various things }, throwable -> { // section handles request errors }); return xyzapiresponse.ok; } }
we new spring , having trouble writing unit test small code snippet.
is there elegant (reactive) way mock webclient or start mock server webclient can use endpoint?
i think build-in spring support still under way - https://jira.spring.io/browse/spr-15286
i wiremock (integration-)test such scenarios. because test whole serialization , deserialization this. wiremock start server serves requests using predefined stubs.
Comments
Post a Comment