Java 如何在Spring云契约存根上执行WireMock.verify()操作?

Java 如何在Spring云契约存根上执行WireMock.verify()操作?,java,spring,spring-boot,spring-cloud,spring-cloud-contract,Java,Spring,Spring Boot,Spring Cloud,Spring Cloud Contract,我正在使用SpringBoot编写一套微服务,我需要运行一些BDD风格的集成测试,这些测试相互独立。为了弄清楚是怎么回事,我在其中一个制作人身上用SpringCloudContract编写了一份非常简单的合同。这是: org.springframework.cloud.contract.spec.Contract.make { description("Contract for the command endpoint where a consumer can send the

我正在使用SpringBoot编写一套微服务,我需要运行一些BDD风格的集成测试,这些测试相互独立。为了弄清楚是怎么回事,我在其中一个制作人身上用SpringCloudContract编写了一份非常简单的合同。这是:

    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,因此我对runner进行了如下设置:

@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");
    }

}
当我将我的消费者指向它时,它发送的请求很好——我可以在控制台输出中看到存根接收到它。但是,我现在要做的是类似于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与SpringCloudContract结合使用,我还需要做一些设置,但我不确定如何做到这一点。我试图在文档中找到一些信息,但我承认我迷路了。我真的很感激任何帮助

有趣

如果使用动态端口,您可以尝试通过
@StubRunnerPort(“myConsumer”)int stubPort
或stub Runner规则或扩展来检索给定存根的URI,然后调用
new WireMock(“localhost”,stubPort)。验证(…)

由于您有一个静态端口6565,您是说执行“newwiremock”(“localhost”,6565)。验证(…)对您不起作用


您可以在这里查看一个示例

我对WireMock非常不熟悉,甚至不知道如何尝试。我只是在尝试我在WireMock文档中读到的东西,基本上是调用WireMock.verify(),而根本不创建实例。我试试看,然后再报告。谢谢这确实是解决办法。我非常感谢你的帮助。这让我快发疯了。谢谢如果您对SpringCloud合同感兴趣,也许您愿意在文档中添加一部分,介绍如何验证存根?当然,我想这是我至少能做的。