【发布时间】: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