|
该版本仍在开发中,尚未被视为稳定。对于最新的稳定版本,请使用 spring-cloud-contract 5.0.0! |
使用上下文路径
Spring Cloud Contract 支持上下文路径。
|
完全支持上下文路径所需的唯一变化是对
制作人方面。此外,自动生成的测试必须使用显式模式。消费者
侧面保持原样。为了让生成的测试通过,你必须使用 explicit
模式。以下示例展示了如何将测试模式设置为 梅文
格拉德勒
|
这样,你就能生成一个不使用 MockMvc 的测试。这意味着你会生成 你需要设置生成测试的基类来处理 Real 请求 插座。
请考虑以下合同:
org.springframework.cloud.contract.spec.Contract.make {
request {
method 'GET'
url '/my-context-path/url'
}
response {
status OK()
}
}
以下示例展示了如何设置基类和 RestAssured:
import io.restassured.RestAssured;
import org.junit.Before;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
@SpringBootTest(classes = ContextPathTestingBaseClass.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ContextPathTestingBaseClass {
@LocalServerPort int port;
@Before
public void setup() {
RestAssured.baseURI = "http://localhost";
RestAssured.port = this.port;
}
}
如果你这样做:
-
你在自动生成测试中的所有请求都会被发送到真实端点,并附带你的 上下文路径包括(例如,
/我的上下文路径/URL). -
你的合同会反映出你有一个上下文路径。你生成的存根还有 这些信息(例如,在小作品中,你必须调用
/我的上下文路径/URL).