我们是否需要在 Spring 云合同中存根其他微服务

Do we need to stub the other micro service in Spring cloud contract

@marcin

我正在做一个试点,为微服务实施 spring 云合同,其中有大约 50 多个服务相互通信。我有几个问题没有在您的文档中找到准确的答案。

我正在构建的服务有一个控制器,它处理我的输入负载并将其转换为 json 格式的所需输出。此 json 用于构建应与 groovy(我们的合约)中的响应相匹配的所需结构。然而,控制器正在向另一个服务发送 json 和一些 URL,如下所示。

request_url=http://localhost:8090/services/rest/transact/v2/pay/validate/0000118228/new response_body=null

基本上它期望通过使用此 json 从其他服务返回响应,现在 response_body=null

我的问题 是我需要创建存根或模拟服务吗?使用此响应作为输入以从响应中产生预期的输出。基本上微服务期待 ServiceResponse.

另一个问题是我们需要在进行合约测试时加载内存数据还是只需要测试控制器本身?

我不太理解你的描述……"The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format"。从哪个 groovy 发送? Groovy申请?你能更深入地解释一下吗?

但我想我还是可以尝试回答这个问题...

My question is do I need to create a stub or mock the service? to make use of this response as input to produce expected output from the response. It is expecting a ServiceResponse.

如果我没理解错 - service 你是说 class 不是应用程序?如果是这样的话,是的,在控制器中我会注入存根服务。

Another question is do we need to load in-memory data while doing the contract testing or do we need to just test the controller itself?

这与上一个答案有关。您的控制器不会将工作委托给服务的任何实际实现,因此不会发生对数据库的访问。如果您查看示例 (https://github.com/spring-cloud-samples/spring-cloud-contract-samples/blob/master/producer/src/test/java/com/example/BeerRestBase.java),您会看到基础 class 已注入模拟并且没有发生真正的集成

编辑:

"The service which I am building has controller which transforms my input payload sent from groovy and giving the desired output in json format" 实际上是对通过 Spring Cloud Contract 生成的测试所做的事情的描述。下一句是

However the controller, is sending json to another services with some URL as shown below.

在合同测试中,我不关心你的控制器进一步做了什么。如果它在您向其他应用程序发送请求的控制器中,那么您应该将其包装在服务 class 中。然后,您将在合同测试中模拟这样的服务。我们在 Contract 测试中关心的是我们是否可以通信。不是整个端到端功能是否正常工作。