讨论:基于 CDC 的生产者测试应该模拟业务逻辑还是与业务逻辑集成
Discussion: Should CDC based producer tests mock business logic or integrate with business logic
我的问题解决了消费者驱动合同 (CDC) 的概念问题,而不是 Spring-Cloud-Contract 特别是(尽管以其他方式标记)。
CDC 通常保证 服务 (producer/supplier) 对其客户端(消费者)的特定行为。许多架构(例如 Hexagonal/Clean/Onion 架构)有一个 adapter/layer 来提供服务的技术接口(REST/SOAP/Messaging/...)。它们将适配器与业务逻辑分开。当基于 CDC 编写测试时,可以只测试适配器(经典单元测试)并模拟业务逻辑。
但是,合同正式地不仅指定了结果(从业务逻辑)到适配器 return 数据传输对象的特定映射。此外,它包含语义。到 request/response 行为:如果消费者发送带有有意义数据的特定请求,生产者应以特定的 正确 答案(相对于请求)进行响应。
我discussion/question的观点是:生产者端的测试是否应该是一个集成测试(集成业务逻辑)以检查业务逻辑是否会return正确答案?在这种情况下,只要业务逻辑是测试用例的组成部分,就可以模拟下一层(业务逻辑后面)。
在 Spring CDC 中(但我认为在其他 CDC 实现中也是如此)可以区分消费者和生产者端以及生成的测试用例。例如,可以减少生产者部分以检查响应的形式正确性(例如,是否包含特定数据项并根据模式进行验证),同时它为消费者部分指定具体响应。这可能会导致消费者方面(并得到消费者测试的支持)的(错误)理解,即如果消费者从 CDC 发送了指定请求,则具体指定数据将由生产者 return 编辑。
CDC 的用户怎么看?您如何指定行为以及您在生产者端对其进行何种程度的测试?
我们在文档中对其进行了描述 - https://docs.spring.io/spring-cloud-contract/docs/2.2.5.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-cloud-contract-purposes。为了方便大家写在这里
Spring Cloud Contract’s purpose is NOT to start writing business
features in the contracts. Assume that we have a business use case of
fraud check. If a user can be a fraud for 100 different reasons, we
would assume that you would create two contracts, one for the positive
case and one for the negative case. Contract tests are used to test
contracts between applications and not to simulate full behavior.
如果您查看 Pact 的文档 (https://docs.pact.io/faq#do-i-still-need-end-to-end-tests),您会发现
Contract tests replace a certain class of system integration test (the
ones you do to make sure that you're using the API correctly and that
the API responds the way you expect). They don't replace the tests
that ensure that the core business logic of your services is working.
在 Spring 云合同的情况下 - 如果您有一个将工作委托给服务的控制器,那么您应该模拟该服务并仅测试您的控制器是否可以接受请求并发回适当的请求回应
我的问题解决了消费者驱动合同 (CDC) 的概念问题,而不是 Spring-Cloud-Contract 特别是(尽管以其他方式标记)。
CDC 通常保证 服务 (producer/supplier) 对其客户端(消费者)的特定行为。许多架构(例如 Hexagonal/Clean/Onion 架构)有一个 adapter/layer 来提供服务的技术接口(REST/SOAP/Messaging/...)。它们将适配器与业务逻辑分开。当基于 CDC 编写测试时,可以只测试适配器(经典单元测试)并模拟业务逻辑。
但是,合同正式地不仅指定了结果(从业务逻辑)到适配器 return 数据传输对象的特定映射。此外,它包含语义。到 request/response 行为:如果消费者发送带有有意义数据的特定请求,生产者应以特定的 正确 答案(相对于请求)进行响应。
我discussion/question的观点是:生产者端的测试是否应该是一个集成测试(集成业务逻辑)以检查业务逻辑是否会return正确答案?在这种情况下,只要业务逻辑是测试用例的组成部分,就可以模拟下一层(业务逻辑后面)。
在 Spring CDC 中(但我认为在其他 CDC 实现中也是如此)可以区分消费者和生产者端以及生成的测试用例。例如,可以减少生产者部分以检查响应的形式正确性(例如,是否包含特定数据项并根据模式进行验证),同时它为消费者部分指定具体响应。这可能会导致消费者方面(并得到消费者测试的支持)的(错误)理解,即如果消费者从 CDC 发送了指定请求,则具体指定数据将由生产者 return 编辑。
CDC 的用户怎么看?您如何指定行为以及您在生产者端对其进行何种程度的测试?
我们在文档中对其进行了描述 - https://docs.spring.io/spring-cloud-contract/docs/2.2.5.RELEASE/reference/htmlsingle/#getting-started-introducing-spring-cloud-contract-purposes。为了方便大家写在这里
Spring Cloud Contract’s purpose is NOT to start writing business features in the contracts. Assume that we have a business use case of fraud check. If a user can be a fraud for 100 different reasons, we would assume that you would create two contracts, one for the positive case and one for the negative case. Contract tests are used to test contracts between applications and not to simulate full behavior.
如果您查看 Pact 的文档 (https://docs.pact.io/faq#do-i-still-need-end-to-end-tests),您会发现
Contract tests replace a certain class of system integration test (the ones you do to make sure that you're using the API correctly and that the API responds the way you expect). They don't replace the tests that ensure that the core business logic of your services is working.
在 Spring 云合同的情况下 - 如果您有一个将工作委托给服务的控制器,那么您应该模拟该服务并仅测试您的控制器是否可以接受请求并发回适当的请求回应