如何对 Spring 云合同存根执行 WireMock.verify() 操作?
How can I do a WireMock.verify() operation on a Spring Cloud Contract stub?
我正在使用 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()
}
}
在消费者方面,我得到了一个存根 运行ning 就好了。我在我的集成测试中使用 Cucumber,所以我设置了 运行ner 像这样:
@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 结合使用,但我不确定该怎么做。我试图在文档中找到一些信息,但我确实迷路了。如果有任何帮助,我将不胜感激!
有趣...
如果您使用动态端口,您可以尝试做的是通过 @StubRunnerPort("myConsumer") int stubPort
或 Stub Runner 规则或扩展检索给定存根的 URI,然后调用 new WireMock("localhost", stubPort).verifyThat(...)
。
既然你有一个静态端口 6565,你是说做 `new WireMock("localhost", 6565).verifyThat(...) 对你不起作用?
您可以在此处查看示例 https://github.com/spring-cloud/spring-cloud-contract/issues/457
我正在使用 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()
}
}
在消费者方面,我得到了一个存根 运行ning 就好了。我在我的集成测试中使用 Cucumber,所以我设置了 运行ner 像这样:
@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 结合使用,但我不确定该怎么做。我试图在文档中找到一些信息,但我确实迷路了。如果有任何帮助,我将不胜感激!
有趣...
如果您使用动态端口,您可以尝试做的是通过 @StubRunnerPort("myConsumer") int stubPort
或 Stub Runner 规则或扩展检索给定存根的 URI,然后调用 new WireMock("localhost", stubPort).verifyThat(...)
。
既然你有一个静态端口 6565,你是说做 `new WireMock("localhost", 6565).verifyThat(...) 对你不起作用?
您可以在此处查看示例 https://github.com/spring-cloud/spring-cloud-contract/issues/457