Provider Contract Testing with Stubs in Artifactory in a Non-JVM World

在此流程中,我们假设:

  • API生产者和API消费者是非JVM应用程序。

  • 合约定义用YAML编写。

  • 存根存储是Artifactory或Nexus。

  • 使用了Spring Cloud Contract Docker(SCC Docker)和Spring Cloud Contract Stub Runner Docker(SCC Stub Runner Docker)映像。

您可以阅读更多有关如何结合 Docker 使用 Spring Cloud Contract 的信息 hereHere,您可以阅读有关如何在多语种环境中使用 Spring Cloud Contract 的博客文章。 Here,您可以找到一个 NodeJS 应用程序示例,它同时将 Spring Cloud Contract 用作生产者和使用者。

Producer Flow

在高层面,生产者:

  1. 写入合约定义(例如,YAML)。

  2. 设置构建工具以:[style="loweralpha"]

    1. 在指定端口上使用模拟服务启动应用程序。如果无法进行模拟,你可以以有状态的方式设置基础设施和定义测试。

    2. 运行Spring Cloud Contract Docker映像,并将正在运行的应用程序的端口作为环境变量传递。SCC Docker映像:

      • 从已连接卷中生成测试。

      • 对正在运行的应用程序运行测试。

测试完成后,存根将上传到存根存储网站(例如 Artifactory 或 Git)。

以下 UML 图显示了生产商流程:

"API Producer"->"API Producer": write contract definitions
"API Producer"->"API Producer": (preferable) prepare a way\nto run the app\nwith mocked services
"API Producer"->"API Producer\nbuild": run the build
"API Producer\nbuild"->"API Producer\nrunning app": run the app\non port X\nwith mocked services
"API Producer\nbuild"->"SCC Docker": attach contract definitions\nas a volume
"API Producer\nbuild"->"SCC Docker": set environment variables\ne.g. app running on port X
"API Producer\nbuild"->"SCC Docker": run the contract tests
"SCC Docker"->"SCC Docker\nimage": run the contract tests
"SCC Docker\nimage"->"SCC Docker\nimage": pick the contract definitions\nfrom volume
"SCC Docker\nimage"->"SCC Docker\nimage": generate contract tests
"SCC Docker\nimage"->"SCC Docker\nimage": run the tests\nagainst app running\non port X
"SCC Docker\nimage"->"SCC Docker\nimage": the tests are passing!
"SCC Docker\nimage"->"Stub Storage": upload the stubs
"SCC Docker\nimage"->"SCC Docker": build successful
"SCC Docker"->"API Producer\nbuild": build successful
"API Producer\nbuild"->"API Producer": build successful

Consumer Flow

在高层面,消费者:

  1. 设置构建工具以:

    • 启动 Spring Cloud Contract Stub Runner Docker 镜像并启动 Stub。环境变量配置:

    • The stubs to fetch.

    • 存储库的位置。请注意:

    • 若要使用本地存储,你还可以将其作为卷连接。

    • 需要公开 Stub 正在运行的端口。

  2. 对正在运行的 Stub 运行应用程序测试。

以下 UML 图显示了消费者流程:

"API Consumer"->"API Consumer\nbuild": run the build
"API Consumer\nbuild"->"SCC\nStub Runner\nDocker": set environment variables\ne.g. stub X running on port Y
"SCC\nStub Runner\nDocker"->"SCC\nStub Runner\nDocker\nimage": fetch and run\nthe stubs
"SCC\nStub Runner\nDocker\nimage"->"Stub Storage": fetch the stubs of X
"Stub Storage"->"SCC\nStub Runner\nDocker\nimage": stubs found
"SCC\nStub Runner\nDocker\nimage"->"X Stub": run the stub of X
"X Stub"->"SCC\nStub Runner\nDocker\nimage": stub is running\non port Y
"SCC\nStub Runner\nDocker\nimage"->"SCC\nStub Runner\nDocker": stubs running and\nready for tests
"API Consumer\nbuild"->"API Consumer\nbuild": run tests against X stub
"API Consumer\nbuild"->"X Stub": send a request
"X Stub"->"API Consumer\nbuild": response received
"API Consumer\nbuild"->"API Consumer": build successful