简体   繁体   English

如何在 Spring Cloud Contract 存根上执行 WireMock.verify() 操作?

[英]How can I do a WireMock.verify() operation on a Spring Cloud Contract stub?

I'm writing a suite of microservices using Spring Boot, and I need to run some BDD-style integration tests that test each one independent of the other.我正在使用 Spring Boot 编写一套微服务,我需要运行一些 BDD 风格的集成测试,以独立测试每个微服务。 In order to figure out how, I have written a very simple contract using Spring Cloud Contract on one of the producers.为了弄清楚如何,我在其中一个生产者上使用 Spring Cloud Contract 编写了一个非常简单的合同。 Here it is:这里是:

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

On the consumer side, I've gotten a stub running just fine.在消费者方面,我有一个运行良好的存根。 I'm using Cucumber in my integration tests, so I've setup the runner like so:我在我的集成测试中使用 Cucumber,所以我已经像这样设置了运行器:

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


}

And I'm setting up the Spring application context like this:我正在像这样设置 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");
    }

}

When I point my consumer to http://localhost:6565 it sends the request just fine - I can see the stub receive it in the console output.当我将我的消费者指向http://localhost:6565时,它会很好地发送请求——我可以看到存根在控制台输出中接收到它。 However, what I want to do now is something akin to a WireMock.verify() operation.但是,我现在想要做的是类似于 WireMock.verify() 操作的事情。 I want my integration test to verify that the stub received a request on the correct endpoint with the correct request body.我希望我的集成测试能够验证存根是否在正确的端点上收到了具有正确请求主体的请求。 However, when I attempt to simply do:但是,当我尝试简单地执行以下操作时:

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

I get this error after a rather lengthy delay:经过相当长的延迟后,我收到此错误:

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)

I imagine there's some more setup I need to do in order to use WireMock in conjunction with Spring Cloud Contract, but I'm not certain how to do it.我想我需要做一些更多的设置才能将 WireMock 与 Spring Cloud Contract 结合使用,但我不确定如何去做。 I've attempted to find some info in the docs, but I'm admittedly lost.我试图在文档中找到一些信息,但我确实迷路了。 I'd really appreciate any help!我真的很感激任何帮助!

Interesting...有趣的...

What you can try to do if you're using dynamic ports, is to retrieve the URI to the given stub either via @StubRunnerPort("myConsumer") int stubPort or Stub Runner Rule or Extension and then call new WireMock("localhost", stubPort).verifyThat(...) .如果您使用的是动态端口,您可以尝试做的是通过@StubRunnerPort("myConsumer") int stubPort或 Stub Runner Rule 或 Extension 检索给定存根的 URI,然后调用new WireMock("localhost", stubPort).verifyThat(...)

Since you have a static port 6565, you're saying that doing `new WireMock("localhost", 6565).verifyThat(...) doesn't work for you?因为你有一个静态端口 6565,你是说做 `new WireMock("localhost", 6565).verifyThat(...) 对你不起作用?

You can check for an example here https://github.com/spring-cloud/spring-cloud-contract/issues/457您可以在此处查看示例https://github.com/spring-cloud/spring-cloud-contract/issues/457

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法使用 Spring 云合约 Wiremock,无法加载 ApplicationContext - Can not use Spring Cloud Contract Wiremock, Failed to load ApplicationContext 如何启用 Spring Cloud Contract Wiremock 服务器日志记录? - How to enable Spring Cloud Contract Wiremock server logging? 我们是否需要在Spring Cloud Contract中存根其他微服务 - Do we need to stub the other micro service in Spring cloud contract 如何在Spring Cloud Contract存根上获取服务状态 - How to get service state on spring cloud contract stub Spring Cloud Contract 无法启动 Stub Runner - Spring Cloud Contract failed to start Stub Runner 如何让 Spring Cloud Contract 在每次测试之前或之后重置 WireMock - How to make Spring Cloud Contract reset WireMock before or after each test 用于 spring-cloud-contract 的自定义存根生成器 - Custom stub generator for spring-cloud-contract spring-cloud-contract-wiremock | 自动注册存根与以编程方式注册 - spring-cloud-contract-wiremock | Registering Stubs Automatically vs Programmatically 有没有办法以编程方式将存根 ID 添加到 Spring Cloud Contract Stub Runner? - Is there a way to programmatically add stub ID's to Spring Cloud Contract Stub Runner? 如何验证对相同 url 但在 Wiremock 中具有不同主体的两个发布请求? - How do I verify two post requests to the same url but with different bodies in Wiremock?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM