最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

spring boot - Wiremock failed on simple call - Stack Overflow

programmeradmin1浏览0评论

I tried to mock three APIs provided by ShortPixel with Wiremock.

  • /
  • /

For the simplest read domain API,

To check the status of a domain and its quota, you should use the following.

The statuses can be

  • 2 = All OK
  • 1 = Credits almost exhausted
  • -1 = Credits exhausted
  • -2 = Credits used up some time ago (can be used e.g. to stop using CDN links)
  • -3 = Domain not reachable

I created the following client codes for it.

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)
    }

There is a little specificity here. I put the above codes into a @PostConstruct method to check the domain's existence. If the domain is not bound to the APIKEY, then call /add-domain to bind it.

When using the following stub to mock the request/response.

stubFor(
    get(urlPathEqualTo(readDomainPath))
        .willReturn(
            aResponse()
                .withStatus(200)
                .withBody("-3")
        )
)

When running the test I got the exception info like:

 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."

I am afraid the Wiremock server is not ready when the component is initialized.

发布评论

评论列表(0)

  1. 暂无评论