提供者 pactVerify 未获取 JSON Pact 文件

Provider pactVerify isn't picking up JSON Pact file

我在同一个 repo 中有两个项目,具有完全独立的目录结构(消费者在 /test-consumer 中,提供者在 /app 中)。

消费者检查在 /test-consumer/build/pacts 中输出一个 JSON Pact 文件,正如

所期望的
dependencies { test { systemProperties['pact.rootDir'] = "$buildDir/pacts" } }

然后我将文件复制到 /app/build/pacts/,并将相同的 systemProperties 行放入我的提供商的 build.gradle

我抄袭的示例项目使用的是 Pact 代理,所以我想我可以把它去掉,并用 rootDir 替换它,但它不起作用。这是我得到的:

WARNING: There are no consumers to verify for provider 'Coffee Ordering Provider'

因此,它似乎正在查找 Pact 文件,但在其中任何一个文件中都找不到提供者+消费者对。

长话短说: 我做错了什么?

这里有一些代码位可以提供帮助:

dependencies {
  ...
  test { systemProperties['pact.rootDir'] = "$buildDir/pacts" }
}

pact {
  serviceProviders {
    'Coffee Ordering Provider' {
      port = 8080

      startProviderTask = startProvider
      terminateProviderTask = stopProvider
      stateChangeUrl = url('http://localhost:8080/pactStateChange')
    }
  }
}

您收到该警告是因为您没有告诉协议插件在哪里可以找到协议文件。对于目录中的契约,添加以下内容:

pact {
  serviceProviders {
    'Coffee Ordering Provider' {
      port = 8080

      startProviderTask = startProvider
      terminateProviderTask = stopProvider
      stateChangeUrl = url('http://localhost:8080/pactStateChange')

      hasPactsWith('Coffee Ordering Consumers') {

          // Will define a consumer for each pact file in the directory.
          // Consumer name is read from contents of pact file
          pactFileLocation = file("$buildDir/pacts")

      }
    }
  }
}

请注意,您为所有测试设置了 pact.rootDir,但协议验证并未 运行 作为测试。