【问题标题】:Seperate Pact Interactions in Provider Verification Tests by Endpoint按端点分离提供者验证测试中的 Pact 交互
【发布时间】:2020-02-11 10:14:26
【问题描述】:

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

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

【问题讨论】:

    标签: pact pact-jvm


    【解决方案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 为例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多