Provider Contract Testing with Stubs in Artifactory in a Non-JVM World
在此流程中,我们假设:
In this flow, we assume that:
-
API生产者和API消费者是非JVM应用程序。
-
The API Producer and API Consumer are non-JVM applications.
-
合约定义用YAML编写。
-
The contract definitions are written in YAML.
-
存根存储是Artifactory或Nexus。
-
The Stub Storage is Artifactory or Nexus.
-
使用了Spring Cloud Contract Docker(SCC Docker)和Spring Cloud Contract Stub Runner Docker(SCC Stub Runner Docker)映像。
-
Spring Cloud Contract Docker (SCC Docker) and Spring Cloud Contract Stub Runner Docker (SCC Stub Runner Docker) images are used.
您可以阅读更多有关如何结合 Docker 使用 Spring Cloud Contract 的信息 here。
You can read more about how to use Spring Cloud Contract with Docker here.
Here,您可以阅读有关如何在多语种环境中使用 Spring Cloud Contract 的博客文章。
Here, you can read a blog post about how to use Spring Cloud Contract in a polyglot world.
Here,您可以找到一个 NodeJS 应用程序示例,它同时将 Spring Cloud Contract 用作生产者和使用者。
Here, you can find a sample of a NodeJS application that uses Spring Cloud Contract both as a producer and a consumer.
Producer Flow
在高层面,生产者:
At a high level, the producer:
-
写入合约定义(例如,YAML)。
-
Writes contract definitions (for example, in YAML).
-
设置构建工具以:[style="loweralpha"]
-
在指定端口上使用模拟服务启动应用程序。[.iokays-translated-cd86f4b15abbf903d6097b3848fe10e2] 如果无法进行模拟,你可以以有状态的方式设置基础设施和定义测试。
If mocking is not possible, you can set up the infrastructure and define tests in a stateful way.
-
Start the application with mocked services on a given port.[.iokays-translated-cd86f4b15abbf903d6097b3848fe10e2] 如果无法进行模拟,你可以以有状态的方式设置基础设施和定义测试。
If mocking is not possible, you can set up the infrastructure and define tests in a stateful way.
-
运行Spring Cloud Contract Docker映像,并将正在运行的应用程序的端口作为环境变量传递。SCC Docker映像:
-
从已连接卷中生成测试。
-
Generates the tests from the attached volume.
-
对正在运行的应用程序运行测试。
-
Runs the tests against the running application.
-
-
Run the Spring Cloud Contract Docker image and pass the port of a running application as an environment variable. The SCC Docker image:
-
从已连接卷中生成测试。
-
Generates the tests from the attached volume.
-
对正在运行的应用程序运行测试。
-
Runs the tests against the running application.
-
-
Sets up the build tool to:[style="loweralpha"]
-
在指定端口上使用模拟服务启动应用程序。[.iokays-translated-cd86f4b15abbf903d6097b3848fe10e2] 如果无法进行模拟,你可以以有状态的方式设置基础设施和定义测试。
If mocking is not possible, you can set up the infrastructure and define tests in a stateful way.
-
Start the application with mocked services on a given port.[.iokays-translated-cd86f4b15abbf903d6097b3848fe10e2] 如果无法进行模拟,你可以以有状态的方式设置基础设施和定义测试。
If mocking is not possible, you can set up the infrastructure and define tests in a stateful way.
-
运行Spring Cloud Contract Docker映像,并将正在运行的应用程序的端口作为环境变量传递。SCC Docker映像:
-
从已连接卷中生成测试。
-
Generates the tests from the attached volume.
-
对正在运行的应用程序运行测试。
-
Runs the tests against the running application.
-
-
Run the Spring Cloud Contract Docker image and pass the port of a running application as an environment variable. The SCC Docker image:
-
从已连接卷中生成测试。
-
Generates the tests from the attached volume.
-
对正在运行的应用程序运行测试。
-
Runs the tests against the running application.
-
测试完成后,存根将上传到存根存储网站(例如 Artifactory 或 Git)。
Upon test completion, stubs get uploaded to a stub storage site (such as Artifactory or Git).
以下 UML 图显示了生产商流程:
The following UML diagram shows the producer flow:
"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
在高层面,消费者:
At a high level, the consumer:
-
设置构建工具以:
-
启动 Spring Cloud Contract Stub Runner Docker 镜像并启动 Stub。[.iokays-translated-e3da7700fa15fceb4506308cfbdb4888] 环境变量配置:
-
The environment variables configure:
-
Start the Spring Cloud Contract Stub Runner Docker image and start the stubs.[.iokays-translated-e3da7700fa15fceb4506308cfbdb4888] 环境变量配置:
The environment variables configure:
-
The stubs to fetch.
-
存储库的位置。[.iokays-translated-2cd5703f5228fa0f35f394a6c8529540] 请注意:
Note that:
-
The location of the repositories.[.iokays-translated-2cd5703f5228fa0f35f394a6c8529540] 请注意:
Note that:
-
若要使用本地存储,你还可以将其作为卷连接。
-
To use the local storage, you can also attach it as a volume.
-
需要公开 Stub 正在运行的端口。
-
The ports at which the stubs are running need to be exposed.
-
Sets up the build tool to:
-
启动 Spring Cloud Contract Stub Runner Docker 镜像并启动 Stub。[.iokays-translated-e3da7700fa15fceb4506308cfbdb4888] 环境变量配置:
-
The environment variables configure:
-
Start the Spring Cloud Contract Stub Runner Docker image and start the stubs.[.iokays-translated-e3da7700fa15fceb4506308cfbdb4888] 环境变量配置:
The environment variables configure:
-
The stubs to fetch.
-
存储库的位置。[.iokays-translated-2cd5703f5228fa0f35f394a6c8529540] 请注意:
Note that:
-
The location of the repositories.[.iokays-translated-2cd5703f5228fa0f35f394a6c8529540] 请注意:
Note that:
-
若要使用本地存储,你还可以将其作为卷连接。
-
To use the local storage, you can also attach it as a volume.
-
需要公开 Stub 正在运行的端口。
-
The ports at which the stubs are running need to be exposed.
-
对正在运行的 Stub 运行应用程序测试。
-
Run the application tests against the running stubs.
以下 UML 图显示了消费者流程:
The following UML diagram shows the consumer flow:
"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