java 在Spock Frameowork中编写集成测试时,如何模拟SpringWebClient调用并存根一些数据

hrysbysz  于 2023-03-21  发布在  Java
关注(0)|答案(1)|浏览(100)

我正在spock框架中为spring web flux应用程序编写集成测试,我们正在使用WebClient进行外部API调用。如何将WebClient和存根数据模拟到WebClient调用?
我正在使用RequestSpecification类在集成测试中进行API调用。

roqulrg3

roqulrg31#

我可以使用下面的代码解决这个问题:

WebClient webClient

WebClient.RequestBodyUriSpec requestBodyUriSpec = 
Mock(WebClient.RequestBodyUriSpec)
WebClient.ResponseSpec responseSpec = Mock(WebClient.ResponseSpec)

内部测试方法:

webClient.get() >> requestBodyUriSpec
webClient.post() >> requestBodyUriSpec
requestBodyUriSpec.body(_, _) >> requestBodyUriSpec
requestBodyUriSpec.uri(_) >> requestBodyUriSpec
requestBodyUriSpec.headers(_) >> requestBodyUriSpec
requestBodyUriSpec.retrieve() >> responseSpec
responseSpec.onStatus(_, _) >> responseSpec
responseSpec.bodyToMono(_) >>> {response objects to be mocked}

相关问题