通过端点在提供者验证测试中分离 Pact 交互

Seperate Pact Interactions in Provider Verification Tests by Endpoint

我刚开始为我的系统采用 Pact 测试,该系统由一个提供者服务和一个 Angular 前端作为消费者组成。我成功地设置了双方,因此,Angular 应用程序生成了一个(单个)契约文件,其中包含与我的提供商服务的多个端点的许多交互。在提供程序中,我现在确实面临这样的问题,即我的验证测试变得非常庞大且过于复杂,因为我必须在一次测试中使用所有数据模拟我的所有端点,例如:

@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

Using multiple classes for the state change methods

If you have a large number of state change methods, you can split things up by moving them to other classes. There are two ways you can do this: Use interfaces

You can put the state change methods on interfaces and then have your test class implement those interfaces. See StateAnnotationsOnInterfaceTest for an example. Specify the additional classes on the test target

You can provide the additional classes to the test target with the withStateHandler or setStateHandlers methods. See BooksPactProviderTest for an example.