Generating Stubs at Runtime

作为使用者,您可能不想等待生产者完成其实施,然后发布其 stub。此问题的解决方案可以在运行时生成 stub。

As a consumer, you might not want to wait for the producer to finish its implementation and then publish their stubs. A solution to this problem can be generation of stubs at runtime.

作为生产人员,当已定义合约时,需要通过生成的测试以便发布存根。在某些情况下,你可能希望解除对使用者的封锁,以便可以在测试实际通过之前获取存根。在这种情况下,应设置此类合约为正在处理中。可以在 Contracts in Progress 部分下阅读更多与此相关的内容。这样,不会生成测试,但会生成存根。

As a producer, when a contract is defined, you are required to make the generated tests pass in order for the stubs to be published. There are cases where you would like to unblock the consumers so that they can fetch the stubs before your tests actually pass. In this case, you should set such contracts as in-progress. You can read more about this under the Contracts in Progress section. That way, your tests are not generated, but the stubs are generated.

作为使用者,您可以切换开关在运行时生成 stub。Stub Runner 忽略所有现有的 stub 映射,并为所有合同定义生成新的映射。另一个选项是传递 stubrunner.generate-stubs 系统属性。以下示例展示了此类设置:

As a consumer, you can toggle a switch to generate stubs at runtime. Stub Runner ignores all the existing stub mappings and generates new ones for all the contract definitions. Another option is to pass the stubrunner.generate-stubs system property. The following example shows such a setup:

Annotation
@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);