Provider Contract Testing with Stubs in Artifactory in a Non-JVM World
在此流程中,我们假设:
In this flow, we assume that:
-
The API Producer and API Consumer are non-JVM applications.
-
The contract definitions are written in YAML.
-
The Stub Storage is Artifactory or Nexus.
-
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:
-
Writes contract definitions (for example, in YAML).
-
Sets up the build tool to:[style="loweralpha"]
-
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. .. 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:
-
Sets up the build tool to:
-
Start the Spring Cloud Contract Stub Runner Docker image and start the stubs.[.iokays-translated-e3da7700fa15fceb4506308cfbdb4888] 环境变量配置:
-
The environment variables configure: * The stubs to fetch. * The location of the repositories.[.iokays-translated-2cd5703f5228fa0f35f394a6c8529540] 请注意:
Note that: * To use the local storage, you can also attach it as a volume. * The ports at which the stubs are running need to be exposed.
-
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