在运行时生成桩

作为消费者,你可能不想等待生产者完成其实现然后再发布桩。解决这个问题的一种方法是在运行时生成桩。

作为生产者,定义契约后,你需要让生成的测试通过才能发布桩。有些情况下,你希望在使用测试通过之前就让消费者能够获取桩。在这种情况下,你应该将这些契约设置为“进行中 (in-progress)”。你可以在进行中的契约部分阅读更多相关内容。这样一来,你的测试不会生成,但桩会生成。

作为消费者,你可以切换一个开关在运行时生成桩。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
@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 Extension
@RegisterExtension
	public StubRunnerExtension stubRunnerExtension = new StubRunnerExtension()
			.downloadStub("com.example:some-producer")
			.repoRoot("stubs://file://location/to/the/contracts")
			.stubsMode(StubRunnerProperties.StubsMode.REMOTE)
			.withGenerateStubs(true);