|
此版本仍在开发中,目前尚不被视为稳定版本。如需最新稳定版本,请使用 spring-cloud-contract 5.0.2! |
运行时生成存根
作为消费者,您可能并不希望等待生产者完成其开发,然后再发布他们的存根。解决此问题的一种方案是在运行时生成存根。
作为生产者,当定义契约时,您需要确保生成的测试通过,才能发布存根。在某些情况下,您可能希望解除对消费者的阻塞,使其能够在您的测试真正通过之前获取存根。此时,您应将此类契约设置为“进行中”状态。有关此内容的更多信息,请参阅 进行中的契约 部分。这样,您的测试将不会被生成,但存根仍会生成。
作为消费者,您可以切换一个开关以在运行时生成存根。Stub Runner 会忽略所有现有的存根映射,并为所有契约定义生成新的存根。另一种选择是传递 stubrunner.generate-stubs 系统属性。以下示例展示了此类配置:
注解
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/contracts",
ids = "com.example:some-producer",
generateStubs = true)
JUnit 4 规则
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withGenerateStubs(true);
JUnit 5 扩展
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withGenerateStubs(true);