CDC(Consumer Driven Contract)
Application Scenarios
Contract DSL(Domain-Specific Language)
- Groovy
- YAML
- Json
参考示例:
https://github.com/pact-foundation/pact-specification/tree/version-3
Producer
吧
定义测试Controller
1 |
|
定义测试Controller的Contracts
在test的Resources下,新建contracts目录,定义一个contract文件,以json格式
1 | { |
定义测试基类ContractVerifierBase
通过配置build的插件spring-cloud-contract-maven-plugin使测试用例继承该类进行初始化操作
在test的com.example.base下,新建ContractVerifierBase.java
1 | import io.restassured.module.mockmvc.RestAssuredMockMvc; |
需要特别注意的是,这个ContractVerifierBase只能是.java文件,目前不支持Kotlin
pom.xml
Attention: SpringBoot version must be <= 2.0.7.RELEASE
1 | <dependencies> |
Maven spring-cloud-contract-pact Plugin
- packageWithBaseClasses: Defines a package where all the base classes reside. This setting takes precedence over baseClassForTests. The convention is such that, if you have a contract under (for example) src/test/resources/contract/foo/bar/baz/ and set the value of the packageWithBaseClasses property to com.example.base, then Spring Cloud Contract Verifier assumes that there is a BarBazBase class under the com.example.base package. In other words, the system takes the last two parts of the package, if they exist, and forms a class with a Base suffix.
Example
测试基类目录:/test/com/example/contacts,此时
Contract路径:/test/resouces/controller/controller_craete_order.json
生成的Contract测试类 -> ControllerTest
默认继承的基类 -> ControllerBase
Contracs Example
1 | { |
Reference
https://springframework.guru/using-spring-cloud-contract-for-consumer-driven-contracts/
https://martinfowler.com/articles/consumerDrivenContracts.html