运行时生成存根
作为消费者,你可能不希望等待生产者完成其实现后再发布存根。解决这个问题的一个方法是在运行时生成存根。
作为生产者,当定义合同时,您需要使生成的测试通过,以便发布存根。在某些情况下,您可能希望解除消费者的阻塞,以便他们能在您的测试实际通过之前获取存根。在这种情况下,您应将此类合同设置为进行中。您可以在进行中的合同部分了解更多信息。这样,您的测试不会被生成,但存根会被生成。
作为消费者,你可以通过切换开关在运行时生成存根。Stub Runner 会忽略所有现有的存根映射,并为所有契约定义生成新的存根。另一种选择是传递 spring.cloud.contract.stubrunner.generate-stubs 系统属性。以下示例展示了这种设置:
@AutoConfigureStubRunner(
stubsMode = StubRunnerProperties.StubsMode.REMOTE,
repositoryRoot = "stubs://file://location/to/the/contracts",
ids = "com.example:some-producer",
generateStubs = true)
@Rule
public StubRunnerRule rule = new StubRunnerRule()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withGenerateStubs(true);
@RegisterExtension
public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
.downloadStub("com.example:some-producer")
.repoRoot("stubs://file://location/to/the/contracts")
.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
.withGenerateStubs(true);