Java 如何使用JUnit模拟此webClient?

Java 如何使用JUnit模拟此webClient?,java,junit,mockito,call,Java,Junit,Mockito,Call,我试图模仿以下方法: 公共单笔支付(最终字符串oId,最终双倍金额){ 返回网络客户端 .put() .uri(“/order/{oId}/amount/{amount}”,oId,amount) .车身(车身嵌件 .fromObject(PRequest)) .exchange() .平面图( 答复->{ if(response.statusCode().is4xxClientError()){ //调用错误函数 }否则{ 返回响应 .bodyToMono(响应前类) .flatMap(应答前

我试图模仿以下方法:

公共单笔支付(最终字符串oId,最终双倍金额){
返回网络客户端
.put()
.uri(“/order/{oId}/amount/{amount}”,oId,amount)
.车身(车身嵌件
.fromObject(PRequest))
.exchange()
.平面图(
答复->{
if(response.statusCode().is4xxClientError()){
//调用错误函数
}否则{
返回响应
.bodyToMono(响应前类)
.flatMap(应答前->{
返回Mono.just(pResposne)
});
}
}
);    
}
请注意,webClient是一个私有实例。

您可以使用。下面是一个示例,使用以下代码:

服务

类ApiCaller{
私人网络客户端网络客户端;
ApiCaller(网络客户端网络客户端){
this.webClient=webClient;
}
monocallapi(){
返回webClient.put()
.uri(“/api/resource”)
.contentType(MediaType.APPLICATION_JSON)
.header(“授权”、“自定义授权”)
.syncBody(新的SimpleRequestDto())
.retrieve()
.BodyToNo(简单响应.class);
}
}
测试

类ApiCallerTest{
私有最终MockWebServer MockWebServer=新MockWebServer();
private final ApiCaller ApiCaller=新的ApiCaller(WebClient.create(mockWebServer.url(“/”).toString());
@之后
void tearDown()引发IOException{
mockWebServer.shutdown();
}
@试验
void call()引发InterruptedException{
mockWebServer.enqueue(
新的MockResponse()
.setResponseCode(200)
.setHeader(HttpHeaders.CONTENT\u TYPE、MediaType.APPLICATION\u JSON\u值)
.setBody(“{\“y\”:\“y\”的值,\“z\”:789}”)
);
SimpleResponseDto response=apiCaller.callApi().block();
断言(response,is)(不是(nullValue()));
断言(response.getY(),是(“y的值”);
断言(response.getZ(),is(789));
RecordedRequest RecordedRequest=mockWebServer.takeRequest();
//使用MockWebServer提供的方法断言请求头
recordedRequest.getHeader(“授权”).equals(“customAuth”);
DocumentContext context=JsonPath.parse(recordedRequest.getBody().inputStream());
//使用JsonPath库断言请求主体
assertThat(上下文,isJson(allOf(
withJsonPath($.a),是(“值1”),
withJsonPath($.b),是(123))
)));
}
}
您可以使用。下面是一个示例,使用以下代码:

服务

类ApiCaller{
私人网络客户端网络客户端;
ApiCaller(网络客户端网络客户端){
this.webClient=webClient;
}
monocallapi(){
返回webClient.put()
.uri(“/api/resource”)
.contentType(MediaType.APPLICATION_JSON)
.header(“授权”、“自定义授权”)
.syncBody(新的SimpleRequestDto())
.retrieve()
.BodyToNo(简单响应.class);
}
}
测试

类ApiCallerTest{
私有最终MockWebServer MockWebServer=新MockWebServer();
private final ApiCaller ApiCaller=新的ApiCaller(WebClient.create(mockWebServer.url(“/”).toString());
@之后
void tearDown()引发IOException{
mockWebServer.shutdown();
}
@试验
void call()引发InterruptedException{
mockWebServer.enqueue(
新的MockResponse()
.setResponseCode(200)
.setHeader(HttpHeaders.CONTENT\u TYPE、MediaType.APPLICATION\u JSON\u值)
.setBody(“{\“y\”:\“y\”的值,\“z\”:789}”)
);
SimpleResponseDto response=apiCaller.callApi().block();
断言(response,is)(不是(nullValue()));
断言(response.getY(),是(“y的值”);
断言(response.getZ(),is(789));
RecordedRequest RecordedRequest=mockWebServer.takeRequest();
//使用MockWebServer提供的方法断言请求头
recordedRequest.getHeader(“授权”).equals(“customAuth”);
DocumentContext context=JsonPath.parse(recordedRequest.getBody().inputStream());
//使用JsonPath库断言请求主体
assertThat(上下文,isJson(allOf(
withJsonPath($.a),是(“值1”),
withJsonPath($.b),是(123))
)));
}
}

尝试添加您迄今为止尝试的内容,以便我们可以帮助您您好,谢谢您的回复。我只想模仿这一部分。返回webClient.put().uri(“/order/{oId}/amount/{amount}”,oId,amount).body(BodyInserts.fromObject(PRequest)).exchange()尝试添加您迄今为止尝试的内容,以便我们可以帮助您嗨,感谢您的回复。我只想模仿这一部分。返回webClient.put().uri(“/order/{oId}/amount/{amount}”,oId,amount).body(BodyInserts.fromObject(PRequest)).exchange()