0

我刚刚开始对我的系统采用 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)
    }
}

还是我的想法有谬误?谢谢。

4

1 回答 1

1

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。

于 2020-02-14T16:23:20.123 回答