使用 gradle 的 PACT 提供方

Provider side of PACT using gradle

我是 PACT 的新手。我的应用程序是 spring 启动应用程序。消费者在 PACT 经纪人中创建了契约。现在我正在尝试从提供商端进行 运行 测试,并且我正在使用 gradle 插件进行 运行 测试。我有几个问题:

  1. 消费者发布的契约没有指定任何状态。所以这意味着我不必在提供商端使用任何 stateChangeUrl ?

  2. 发布的契约有get方法。但是,当我 运行 pactVerify 服务器启动时,我的 pact 测试失败了,因为我没有任何现有数据。

  3. 之后如何注入数据

我想要实现的是:当我说 pactVerify

下面是我的gradle文件

pact {
    serviceProviders {
        'user-api' {
            protocol = 'http'
            host = 'localhost'
            port = 8080

            startProviderTask = startService
            terminateProviderTask = stopService

            if ('pactVerify' in gradle.startParameter.taskNames) {
                hasPactsFromPactBroker('http://pactbroker.com') {
                < How to write some code here to say do POST before running PACT >

                }

            }

        }
    }

消费者代码需要在契约中声明提供者状态,以允许您为交互设置正确的数据。

来自docs.pact.io

Each interaction in a pact should be verified in isolation, with no context maintained from the previous interactions. Tests that depend on the outcome of previous tests are brittle and land you back in integration test hell, which is the nasty place you're trying to escape by using pacts.

So how do you test a request that requires data to already exist on the provider? Provider states allow you to set up data on the provider by injecting it straight into the data source before the interaction is run, so that it can make a response that matches what the consumer expects. The name of the provider state is specified in the given clause of an interaction in the consumer, and then used to find the block of code to run in the provider to set up the right data. If you need to stub a downstream system, or return an error response that is difficult to cause in the normal scheme of things (e.g. a 500), this is the place where you can set up stubs.

Provider states also allow the consumer to make the same request with different expected responses (e.g. different response codes, or the same resource with a different subset of data).

Keep in mind that a provider state is all about the state of the provider (eg. what data is there, how it is going to handle a given response), not about the state of the consumer, or about what is in the request.

Refer to the the Ruby example to see how this plays out in code.

  1. Pact published by consumer do not have any states specified. So this means I don't have to use any stateChangeUrl at provider side ?

这意味着您已选择不使用此机制来更改状态,因此是的,您不需要使用那个 URL。但您可能希望改变主意。

  1. Pact published has get method. However when I run pactVerify server starts and my pact tests are failing because I don't have any existing data. How can I inject data after.

为什么不在开始所有测试之前注入数据?如果您不需要担心状态,那么您也可以将数据 pre-loaded 启动到数据库中。

如前所述,所有配置信息都可以在 https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-provider-gradle 找到。