简体   繁体   English

按端点在提供商验证测试中分离协议交互

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

I just started to adopt Pact test for my system that consists of one provider service and an Angular frontend as the consumer.我刚刚开始为我的系统采用 Pact 测试,该系统由一个提供者服务和一个作为消费者的 Angular 前端组成。 I succeeded in setting up both sides, thus, the Angular application generates a (single) pact file with many interactions to multiple endpoints of my provider service.我成功地设置了双方,因此,Angular 应用程序生成了一个(单个)pact 文件,其中包含与我的提供者服务的多个端点的许多交互。 In the provider, I do now face the issue that my verification test gets very large and overly complicated since I have to mock all my endpoints in a single test with all their data, eg:在提供程序中,我现在面临的问题是我的验证测试变得非常大且过于复杂,因为我必须在单个测试中使用所有数据模拟我的所有端点,例如:

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

    [...]

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

Is there a way to seperate the interaction from the pact file such that I could have multiple small verification tests in my provider?有没有办法将交互与协议文件分开,以便我可以在我的提供者中进行多个小型验证测试? For instance, I would like to have a verification test for all requests with path starting with "/example" and a second test for path starting with "/other".例如,我想对路径以“/example”开头的所有请求进行验证测试,并对以“/other”开头的路径进行第二次测试。

So what I would prefer to have is smaller, focused verification test like so:所以我更喜欢更小、更集中的验证测试,如下所示:

@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)
    }
}

Or do I have a fallacy in my thinking?还是我的思维有误? Thanks.谢谢。

The JUnit4 Readme has a section about this topic. JUnit4 自述文件有一个关于这个主题的部分。 I think it also applies to Junit5 https://github.com/DiUS/pact-jvm/tree/master/provider/pact-jvm-provider-junit#using-multiple-classes-for-the-state-change-methods我认为它也适用于 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.有关示例,请参阅 StateAnnotationsOnInterfaceTest。 Specify the additional classes on the test target在测试目标上指定其他类

You can provide the additional classes to the test target with the withStateHandler or setStateHandlers methods.您可以使用 withStateHandler 或 setStateHandlers 方法向测试目标提供其他类。 See BooksPactProviderTest for an example.有关示例,请参阅 BooksPactProviderTest。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM