https://no-cdn.shortpixel.ai/read-domain/example.com
当使用以下存根模拟请求/响应时。2=全部OK
1=学分几乎筋疲力尽-1 =学分耗尽-2 =一段时间以前使用的信用(可以使用例如停止使用CDN链接)
以绑定它。
- -3 =域无法到达
- I为其创建了以下客户端代码。
val domainResult = noCdnWebClient .get().uri(readDomainPath) .awaitExchange { if (it.statusCode().isError) { throw ImageOptimizerException("Failed to check domain $domain on shortpixel, ${it.awaitBody<String>()}") } it.awaitBody(Int::class) }
- 这里有一些特殊性。我将上述代码放入检查域存在的方法中。如果域不绑定到apikey,请致电
@PostConstruct
/add-domain
当运行测试时,我得到了例外信息,例如:
stubFor(
get(urlPathEqualTo(readDomainPath))
.willReturn(
aResponse()
.withStatus(200)
.withBody("-3")
)
)
我担心当组件初始化时,WireMock服务器还没有准备好。
问题是在运行测试时,在Reetemock准备就绪之前调用了称为远程API的bean bean。最简单的方法是稍微调整测试代码,使bean在测试中不受弹簧应用程序上下文管理,然后手动创建一个实例。
HTTP GET http://localhost:9000/read-domain/localhost
Response 404 NOT_FOUND
Decoded "No response could be served as there are no stub mappings in this WireMock instance."