在執行時生成樁
作為消費者,你可能不想等待生產者完成其實現然後再發布樁。解決這個問題的一種方法是在執行時生成樁。
作為生產者,定義契約後,你需要讓生成的測試透過才能釋出樁。有些情況下,你希望在使用測試透過之前就讓消費者能夠獲取樁。在這種情況下,你應該將這些契約設定為“進行中 (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);