当前位置: 首页 > 知识库问答 >
问题:

如何在Spring Cloud合同存根上执行wiremock.verify()操作?

司空昊阳
2023-03-14

我正在使用Spring Boot编写一套微服务,我需要运行一些BDD风格的集成测试,这些测试独立于其他测试。为了弄清楚是怎么回事,我已经在其中一个生产者上使用Spring Cloud contract编写了一个非常简单的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 {


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

}
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结合使用,还需要进行一些设置,但我不确定该如何进行。我曾试图在文件中找到一些信息,但我确实迷路了。我真的很感激你的帮助!

共有1个答案

牧献
2023-03-14

有趣...

如果使用动态端口,您可以尝试通过@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中查看示例

 类似资料:
  • 如何在spark SQL上执行分组操作。我不能在一个特定的列上按组,并在不同的列上聚合值

  • 问题内容: 我正在使用自定义的CreateView(CourseCreate)和UpdateView(CourseUpdate)保存和更新课程。保存课程后,我想采取措施。我将在新课程的讲师和用户之间建立新的多对多关系(如果尚不存在)。 因此,我想将“课程”另存为课程,然后使用“ course.faculty”创建该新关系。做到这一点的最佳地点在哪里? 我正在尝试在视图的form_valid中执行此

  • 我有一个server.java文件: 还有一个客户。java文件格式为: 我还创建了自己的证书,如下所示: 但无论我做什么,我总是会犯错误: 我担心的是,如果我将端口从80更改为其他类型,代码工作得非常好,但我必须专门处理这个端口本身。证书和两个java文件位于同一文件夹中。你们能帮我找到一种在端口80上执行SSL操作的方法吗?

  • 例如,您有一个要转换为JSONObject的pojo列表。你有一个POJO的列表。但是为了转换为JSONObject,您需要使用JSONObject put方法。 如果我只想做一个手术,我就可以做

  • 问题内容: 我想使用java8替换以下代码: 以下伪代码由于没有方法而无法使用,但无论如何它说明了我的目的: 问题答案: 对于Java 9或更高版本,最可能需要的是: 使用vavr之类的方法进行固化可能会得到更整齐的代码,但是我还没有尝试过。

  • 我正在尝试根据枚举值检索一个值。基本上,假设我有以下枚举: 通过执行auth.key.get()将返回“MyKey”,而auth.mail.get()将返回“MyMail”。我搜索了一下,但没有找到答案,我之前没有尝试过任何事情,因为我完全不知道如何开始。谢谢,祝你有个愉快的一天