Common Top-Level Elements
Description
可以向合同中添加 description
。说明为任意文本。以下代码显示一个示例:
Unresolved directive in common-top-elements.adoc - include::{contract_spec_tests_path}/src/test/groovy/org/springframework/cloud/contract/spec/internal/ContractSpec.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract_rest.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_with_tags.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt[]
Name
可以为合同提供一个名称。假设你提供以下名称:should register a user
。如果你这样做,自动生成测试的名称为 validate_should_register_a_user
。此外,WireMock 存根中存根的名称为 should_register_a_user.json
。
您必须确保该名称不包含让生成的测试无法编译的任何字符。另外,请记住,如果您对多个合约提供相同的名称,则自动生成的测试将无法编译,而生成的存根将相互覆盖。
以下示例显示如何向合同中添加名称:
Unresolved directive in common-top-elements.adoc - include::{contract_spec_tests_path}/src/test/groovy/org/springframework/cloud/contract/spec/internal/ContractSpec.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_with_tags.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt[]
Ignoring Contracts
如果要忽略合同,则可以在插件配置中设置一个忽略的合同的值,或在合同本身上设置 ignored
属性。以下示例显示如何执行此操作:
Unresolved directive in common-top-elements.adoc - include::{contract_spec_tests_path}/src/test/groovy/org/springframework/cloud/contract/spec/internal/ContractSpec.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_with_tags.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt[]
Contracts in Progress
正在进行的合同不会在生产者一侧生成测试,但允许生成存根。
请谨慎使用该功能,因为它可能导致误报,因为您在没有实际拥有可用实现时为您的消费者生成存根。
如果要将合同设置为进行中,以下示例显示如何执行此操作:
Unresolved directive in common-top-elements.adoc - include::{contract_spec_tests_path}/src/test/groovy/org/springframework/cloud/contract/spec/internal/ContractSpec.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_with_tags.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt[]
可以将 failOnInProgress
Spring Cloud Contract 插件属性的值设置为确保在你的源中至少有一个正在进行的合同时终止你的构建。
Passing Values from Files
从版本 1.2.0
开始,你可以传递文件中的值。假设你的项目中具有以下资源:
└── src
└── test
└── resources
└── contracts
├── readFromFile.groovy
├── request.json
└── response.json
另外假设你的合同如下:
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/classpath/readFromFile.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract_from_file.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_from_file.java[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/kotlin/readFromFile.kts[]
再假设 JSON 文件如下:
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/classpath/request.json[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/classpath/response.json[]
当测试或存根生成发生时,request.json
和 response.json
文件的内容被传递给请求或响应的正文。文件名称需要是驻留合同所在的文件夹的相对位置中的一个文件。
如果你需要以二进制形式传递文件的内容,则可以在编码的 DSL 中使用 fileAsBytes
方法,或在 YAML 中使用 bodyFromFileAsBytes
字段。
以下示例显示如何传递二进制文件的内容:
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/body_builder/worksWithPdf.groovy[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/yml/contract_pdf.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_from_pdf.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/resources/contracts/shouldWorkWithBinaryPayload.kts[]
在您想要处理二进制有效负载时,无论对于 HTTP 还是消息传递,都应该使用这种方法。
Metadata
你可以向你的合同添加 metadata
。通过元数据,你可以将配置传递给扩展。在下面,你可以找到使用 wiremock
键的示例。它的值是一个映射,其键是 stubMapping
,值是 WireMock 的 StubMapping
对象。Spring Cloud Contract 能够用你的自定义代码修补你生成的存根映射的部分。你可能需要这样做,以便添加 Webhook、自定义延迟或与第三方 WireMock 扩展集成。
Unresolved directive in common-top-elements.adoc - include::{standalone_samples_path}/http-server/src/test/resources/contracts/fraud/shouldReturnFraudStats.groovy[]
Unresolved directive in common-top-elements.adoc - include::{standalone_samples_path}/http-server/src/test/resources/contracts/yml/fraud/shouldReturnFraudStats.yml[]
Unresolved directive in common-top-elements.adoc - include::{verifier_root_path}/src/test/resources/contractsToCompile/contract_rest_with_tags.java[]
Unresolved directive in common-top-elements.adoc - include::{contract_kotlin_spec_path}/src/test/kotlin/org/springframework/cloud/contract/spec/ContractTests.kt[]
在以下部分中,你可以找到受支持的元数据项的示例。