簡體   English   中英

按端點在提供商驗證測試中分離協議交互

[英]Seperate Pact Interactions in Provider Verification Tests by Endpoint

我剛剛開始為我的系統采用 Pact 測試,該系統由一個提供者服務和一個作為消費者的 Angular 前端組成。 我成功地設置了雙方,因此,Angular 應用程序生成了一個(單個)pact 文件,其中包含與我的提供者服務的多個端點的許多交互。 在提供程序中,我現在面臨的問題是我的驗證測試變得非常大且過於復雜,因為我必須在單個測試中使用所有數據模擬我的所有端點,例如:

@Provider("example-backend")
@PactFolder("pacts")
@SpringBootTest(...)
class ComprehensivePactTest {

    [...]

    @State("healthy")
    fun `healthy state`() {
        whenever(exampleService.dosomething(any())).thenReturn(exampleResponse)
        whenever(otherService.somethingElse()).thenReturn(otherResponse)
    }
}

有沒有辦法將交互與協議文件分開,以便我可以在我的提供者中進行多個小型驗證測試? 例如,我想對路徑以“/example”開頭的所有請求進行驗證測試,並對以“/other”開頭的路徑進行第二次測試。

所以我更喜歡更小、更集中的驗證測試,如下所示:

@Provider("example-backend")
@PactFolder("pacts")
@SpringBootTest(...)
class ExampleEndpointPactTest {

    [... include some filter logic here ...]

    @State("healthy")
    fun `healthy state`() {
        whenever(exampleService.dosomething(any())).thenReturn(exampleResponse)
    }
}

@Provider("example-backend")
@PactFolder("pacts")
@SpringBootTest(...)
class OtherEndpointPactTest {

    [... include some filter logic here ...]

    @State("healthy")
    fun `healthy state`() {
        whenever(otherService.somethingElse()).thenReturn(otherResponse)
    }
}

還是我的思維有誤? 謝謝。

JUnit4 自述文件有一個關於這個主題的部分。 我認為它也適用於 Junit5 https://github.com/DiUS/pact-jvm/tree/master/provider/pact-jvm-provider-junit#using-multiple-classes-for-the-state-change-methods

將多個類用於狀態更改方法

如果您有大量的狀態更改方法,您可以通過將它們移動到其他類來拆分它們。 有兩種方法可以做到這一點: 使用接口

您可以將狀態更改方法放在接口上,然后讓您的測試類實現這些接口。 有關示例,請參閱 StateAnnotationsOnInterfaceTest。 在測試目標上指定其他類

您可以使用 withStateHandler 或 setStateHandlers 方法向測試目標提供其他類。 有關示例,請參閱 BooksPactProviderTest。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM