|
此版本仍在开发中,目前尚不被视为稳定版本。如需最新稳定版本,请使用 spring-cloud-contract 5.0.2! |
常见顶层元素
描述
您可以在合同中添加一个 description。该描述为任意文本。以下代码显示了一个示例:
org.springframework.cloud.contract.spec.Contract.make {
description('''
given:
An input
when:
Sth happens
then:
Output
''')
}
description: Some description
name: some name
priority: 8
ignored: true
request:
url: /foo
queryParameters:
a: b
b: c
method: PUT
headers:
foo: bar
fooReq: baz
body:
foo: bar
matchers:
body:
- path: $.foo
type: by_regex
value: bar
headers:
- key: foo
regex: bar
response:
status: 200
headers:
foo2: bar
foo3: foo33
fooRes: baz
body:
foo2: bar
foo3: baz
nullValue: null
matchers:
body:
- path: $.foo2
type: by_regex
value: bar
- path: $.foo3
type: by_command
value: executeMe($it)
- path: $.nullValue
type: by_null
value: null
headers:
- key: foo2
regex: bar
- key: foo3
command: andMeToo($it)
Contract.make(c -> {
c.description("Some description");
}));
contract {
description = """
given:
An input
when:
Sth happens
then:
Output
"""
}
姓名
您可以为您的契约提供一个名称。假设您提供了以下名称:should register a user。如果这样做,自动生成的测试名称将是:validate_should_register_a_user。此外,WireMock 模拟(stub)中的名称也将是:should_register_a_user.json。
| 您必须确保名称中不包含任何会导致生成的测试无法编译的字符。此外,请记住,如果您为多个契约提供了相同的名称,那么自动生成的测试将无法编译,且生成的存根会相互覆盖。 |
以下示例展示了如何向合同中添加姓名:
org.springframework.cloud.contract.spec.Contract.make {
name("some_special_name")
}
name: some name
Contract.make(c -> {
c.name("some name");
}));
contract {
name = "some_special_name"
}
忽略契约
如果您想忽略某个契约,可以在插件配置中设置被忽略契约的值,或在契约本身上设置 ignored 属性。以下示例展示了如何操作:
org.springframework.cloud.contract.spec.Contract.make {
ignored()
}
ignored: true
Contract.make(c -> {
c.ignored();
}));
contract {
ignored = true
}
合同进行中
正在进行中的契约不会在生产方生成测试,但允许生成存根。
| 请谨慎使用此功能,因为它可能导致误报,因为您为消费者生成了存根(stubs),但实际实现尚未到位。 |
如果您希望设置一个进行中的合同,以下示例展示了如何操作:
org.springframework.cloud.contract.spec.Contract.make {
inProgress()
}
inProgress: true
Contract.make(c -> {
c.inProgress();
}));
contract {
inProgress = true
}
您可以将 failOnInProgress Spring Cloud Contract 插件属性的值设置为确保当您的源代码中至少存在一个处于进行中的契约时,构建会失败。
从文件中传递值
从版本 1.2.0 开始,您可以从文件中传递值。假设您的项目中具有以下资源:
└── src
└── test
└── resources
└── contracts
├── readFromFile.groovy
├── request.json
└── response.json
进一步假设您的合同如下:
/*
* Copyright 2013-present the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
method('PUT')
headers {
contentType(applicationJson())
}
body(file("request.json"))
url("/1")
}
response {
status OK()
body(file("response.json"))
headers {
contentType(applicationJson())
}
}
}
request:
method: GET
url: /foo
bodyFromFile: request.json
response:
status: 200
bodyFromFile: response.json
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import org.springframework.cloud.contract.spec.Contract;
class contract_rest_from_file implements Supplier<Collection<Contract>> {
@Override
public Collection<Contract> get() {
return Collections.singletonList(Contract.make(c -> {
c.request(r -> {
r.url("/foo");
r.method(r.GET());
r.body(r.file("request.json"));
});
c.response(r -> {
r.status(r.OK());
r.body(r.file("response.json"));
});
}));
}
}
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
url = url("/1")
method = PUT
headers {
contentType = APPLICATION_JSON
}
body = bodyFromFile("request.json")
}
response {
status = OK
body = bodyFromFile("response.json")
headers {
contentType = APPLICATION_JSON
}
}
}
进一步假设 JSON 文件如下:
{
"status": "REQUEST"
}
{
"status": "RESPONSE"
}
当测试或存根生成发生时,request.json 和 response.json 文件的内容会被传递到请求或响应的正文部分。文件名需要是位于契约所在文件夹相对路径下的一个文件。
如果您需要以二进制形式传递文件的内容,可以使用编码 DSL 中的 fileAsBytes 方法,或在 YAML 中使用 bodyFromFileAsBytes 字段。
以下示例展示了如何传递二进制文件的内容:
import org.springframework.cloud.contract.spec.Contract
Contract.make {
request {
url("/1")
method(PUT())
headers {
contentType(applicationOctetStream())
}
body(fileAsBytes("request.pdf"))
}
response {
status 200
body(fileAsBytes("response.pdf"))
headers {
contentType(applicationOctetStream())
}
}
}
request:
url: /1
method: PUT
headers:
Content-Type: application/octet-stream
bodyFromFileAsBytes: request.pdf
response:
status: 200
bodyFromFileAsBytes: response.pdf
headers:
Content-Type: application/octet-stream
import java.util.Collection;
import java.util.Collections;
import java.util.function.Supplier;
import org.springframework.cloud.contract.spec.Contract;
class contract_rest_from_pdf implements Supplier<Collection<Contract>> {
@Override
public Collection<Contract> get() {
return Collections.singletonList(Contract.make(c -> {
c.request(r -> {
r.url("/1");
r.method(r.PUT());
r.body(r.fileAsBytes("request.pdf"));
r.headers(h -> {
h.contentType(h.applicationOctetStream());
});
});
c.response(r -> {
r.status(r.OK());
r.body(r.fileAsBytes("response.pdf"));
r.headers(h -> {
h.contentType(h.applicationOctetStream());
});
});
}));
}
}
import org.springframework.cloud.contract.spec.ContractDsl.Companion.contract
contract {
request {
url = url("/1")
method = PUT
headers {
contentType = APPLICATION_OCTET_STREAM
}
body = bodyFromFileAsBytes("contracts/request.pdf")
}
response {
status = OK
body = bodyFromFileAsBytes("contracts/response.pdf")
headers {
contentType = APPLICATION_OCTET_STREAM
}
}
}
| 您应在希望处理二进制负载(包括 HTTP 和消息传递)时采用此方法。 |
元数据
您可以将 metadata 添加到您的契约中。通过元数据,您可将配置传递给扩展程序。下方是使用 wiremock 键的示例。其值是一个映射,其中键为 stubMapping,值为 WireMock 的 StubMapping 对象。Spring Cloud Contract 能够用自定义代码修补生成的存根映射的部分内容。您可能希望这样做,以添加 Webhook、自定义延迟或与第三方 WireMock 扩展进行集成。
Contract.make(c -> {
c.metadata(MetadataUtil.map()
.entry("wiremock", ContractVerifierUtil.map()
.entry("stubMapping", "{ \"response\" : { \"fixedDelayMilliseconds\" : 2000 } }")));
}));
contract {
metadata("wiremock" to ("stubmapping" to """
{
"response" : {
"fixedDelayMilliseconds": 2000
}
}"""))
}
在以下各节中,您可以找到支持的元数据条目的示例。
HTTP 合同
Spring Cloud Contract 允许您验证那些使用 REST 或 HTTP 作为通信方式的应用程序。Spring Cloud Contract 可以验证:对于符合合同中 request 部分所定义条件的请求,服务器所提供的响应是否符合合同中 response 部分的规定。随后,这些合同将用于生成 WireMock 模拟服务(stub),以便对任何符合所提供条件的请求,都能返回适当的响应。