【问题标题】:How can I do a WireMock.verify() operation on a Spring Cloud Contract stub?如何在 Spring Cloud Contract 存根上执行 WireMock.verify() 操作?
【发布时间】:2020-05-09 00:29:30
【问题描述】:

我正在使用 Spring Boot 编写一套微服务,我需要运行一些 BDD 风格的集成测试,这些测试相互独立。为了弄清楚如何,我在其中一个生产者上使用 Spring Cloud Contract 编写了一个非常简单的合约。这里是:

    org.springframework.cloud.contract.spec.Contract.make {
    description("Contract for the command endpoint where a consumer can send the service individual commands")
    name("CommandEndpoint")
    request {
        method 'POST'
        urlPath('/myendpoint')
        headers {
            contentType(applicationJson())
        }
        body(["rootId" : "1234", "reportId" : "7ea6bfba-ec22-4a53-b6e9-9261ee459b69"])
    }
    response {
        status OK()
    }
}

在消费者方面,我的存根运行得很好。我在集成测试中使用 Cucumber,所以我设置了这样的跑步者:

@RunWith(Cucumber.class)
@CucumberOptions(features= {"src/test/resources/features"},
glue = {"bdd/stepDefinitions"},
dryRun = false)
public class CucumberRunnerIT {


}

我正在像这样设置 Spring 应用程序上下文:

@SpringBootTest(webEnvironment=WebEnvironment.NONE, classes = { BddConfig.class })
@AutoConfigureStubRunner(ids = {"com.blah.consumer:my-consumer:0.0.48:stubs:6565"},
    stubsMode = StubRunnerProperties.StubsMode.LOCAL)
public class SpringContextLoader {
    private static final Logger LOGGER = LogManager.getLogger(SpringContextLoader.class);

    @Before
    public void setUp() {
        LOGGER.info("LOADING SPRING CONTEXT");
    }

}

当我将消费者指向http://localhost:6565 时,它会很好地发送请求 - 我可以看到存根在控制台输出中接收到它。但是,我现在想做的是类似于 WireMock.verify() 操作。我希望我的集成测试验证存根在正确的端点上收到了具有正确请求正文的请求。但是,当我尝试简单地做:

verify(postRequestedFor(urlEqualTo("/myendpoint")));

经过相当长的延迟后,我得到了这个错误:

com.github.tomakehurst.wiremock.common.JsonException: {
  "errors" : [ {
    "code" : 10,
    "source" : {
      "pointer" : "/timestamp"
    },
    "title" : "Error parsing JSON",
    "detail" : "Unrecognized field \"timestamp\" (class com.github.tomakehurst.wiremock.common.Errors), not marked as ignorable"
  } ]
}
    at com.github.tomakehurst.wiremock.common.JsonException.fromJackson(JsonException.java:49)
    at com.github.tomakehurst.wiremock.common.Json.read(Json.java:52)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.safelyExecuteRequest(HttpAdminClient.java:449)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.postJsonAssertOkAndReturnBody(HttpAdminClient.java:383)
    at com.github.tomakehurst.wiremock.client.HttpAdminClient.countRequestsMatching(HttpAdminClient.java:222)
    at com.github.tomakehurst.wiremock.client.WireMock.verifyThat(WireMock.java:526)
    at com.github.tomakehurst.wiremock.client.WireMock.verifyThat(WireMock.java:511)
    at com.github.tomakehurst.wiremock.client.WireMock.verify(WireMock.java:549)
    at bdd.stepDefinitions.VerificationToIrsSteps.i_validate_the_outcomes(VerificationToIrsSteps.java:33)
    at ✽.I validate the outcomes(src/test/resources/features/VerificationToIrs.feature:25)

我想我还需要做一些设置才能将 WireMock 与 Spring Cloud Contract 结合使用,但我不确定该怎么做。我试图在文档中找到一些信息,但我承认我迷路了。非常感谢任何帮助!

【问题讨论】:

    标签: java spring spring-boot spring-cloud spring-cloud-contract


    【解决方案1】:

    有趣...

    如果您使用动态端口,您可以尝试通过@StubRunnerPort("myConsumer") int stubPort 或存根运行规则或扩展检索给定存根的URI,然后调用new WireMock("localhost", stubPort).verifyThat(...)

    由于您有一个静态端口 6565,您是说执行 `new WireMock("localhost", 6565).verifyThat(...) 对您不起作用?

    您可以在这里查看示例https://github.com/spring-cloud/spring-cloud-contract/issues/457

    【讨论】:

    • 我实际上对 WireMock 非常陌生,以至于我什至不知道要尝试一下。我只是在尝试我在 WireMock 的文档中阅读的内容,基本上是调用 WireMock.verify() 而不创建实例。我会试一试并报告。谢谢!
    • 这确实是解决方案。我非常感谢您的帮助。这一直让我发疯。谢谢!
    • 如果您有兴趣为 Spring Cloud Contract 做出贡献,也许您愿意添加一段文档来说明如何验证存根?
    • 当然,我认为这是我能做的至少。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-04-18
    • 1970-01-01
    • 1970-01-01
    • 2018-12-27
    • 2017-09-29
    • 2020-10-24
    • 1970-01-01
    相关资源
    最近更新 更多