运行时生成存根
作为消费者,你可能不想等到生产者完成实现后再发布他们的存根。解决这个问题的方法可以通过在运行时生成存根。
作为制片人,当合同确定时,你需要让生成的测试通过,才能发布存根。有些情况下,你希望解除对消费者的阻碍,让他们在测试通过前获取存根。在这种情况下,你应该将这些合同设为进行中。你可以在“合同进行中”部分了解更多相关内容。这样,你的测试不会被生成,但存根会被生成。
作为消费者,你可以切换开关在运行时生成存根。存根运行器忽略所有现有的存根映射,并为所有合同定义生成新的存根映射。另一种选择是通过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)
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);