Stub Runner JUnit Rule and Stub Runner JUnit5 Extension
Stub Runner 带有一个 JUnit 规则,它允许您可以下载并运行给定组和工件 ID 的存根,如下面的示例所示:
Stub Runner comes with a JUnit rule that lets you can download and run stubs for a given group and artifact ID, as the following example shows:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit4/StubRunnerRuleJUnitTest.java[]
JUnit 5 也提供了一个 StubRunnerExtension
。StubRunnerRule
和 StubRunnerExtension
的工作方式非常类似。在调用规则或扩展后,Stub Runner 会连接到你的 Maven 存储库,并尝试针对给定的依赖项列表执行以下操作:
A StubRunnerExtension
is also available for JUnit 5. StubRunnerRule
and
StubRunnerExtension
work in a very similar fashion. After the rule or extension is
called, Stub Runner connects to your Maven repository and, for the given list of
dependencies, tries to:
-
Download them
-
Cache them locally
-
Unzip them to a temporary folder
-
Start a WireMock server for each Maven dependency on a random port from the provided range of ports or the provided port
-
Feed the WireMock server with all JSON files that are valid WireMock definitions
-
Send messages (remember to pass an implementation of
MessageVerifierSender
interface)
Stub Runner 使用 Eclipse Aether 机制下载 Maven 依赖项。查看其 docs 以了解更多信息。
Stub Runner uses the Eclipse Aether mechanism to download the Maven dependencies. Check their docs for more information.
由于 StubRunnerRule
和 StubRunnerExtension
实现了 StubFinder
,你可以像以下示例所示那样查找启动的 Stub:
Since the StubRunnerRule
and StubRunnerExtension
implement the StubFinder
, they let
you find the started stubs, as the following example shows:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/main/java/org/springframework/cloud/contract/stubrunner/StubFinder.java[]
以下示例提供了关于使用 Stub Runner 的更多详细信息:
The following examples provide more detail about using Stub Runner:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit4/StubRunnerRuleSpec.groovy[]
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit4/StubRunnerRuleJUnitTest.java[]
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/java/org/springframework/cloud/contract/stubrunner/junit/StubRunnerJUnit5ExtensionTests.java[]
请参阅 Common Properties for JUnit and Spring 以了解有关如何应用 Stub Runner 的全局配置的更多信息。
See the Common Properties for JUnit and Spring for more information on how to apply global configuration of Stub Runner.
要将junit规则或junit 5扩展与消息传递一起使用,必须为规则生成器(例如`rule.messageVerifierSender(new MyMessageVerifierSender())`)提供`MessageVerifierSender`和`MessageVerifierReceiver`界面的实现。如果不这样做,那么每当尝试发送消息时就会引发异常。
To use the JUnit rule or JUnit 5 extension together with messaging, you have to provide an implementation of the
MessageVerifierSender
and MessageVerifierReceiver
interface to the rule builder (for example, rule.messageVerifierSender(new MyMessageVerifierSender())
).
If you do not do this, then, whenever you try to send a message, an exception is thrown.
Maven Settings
Stub 下载器尊重使用其他本地存储库文件夹的 Maven 设置。目前不会考虑存储库和配置文件的身份验证详细信息,因此你需要使用上述属性来指定它。
The stub downloader honors Maven settings for a different local repository folder. Authentication details for repositories and profiles are currently not taken into account, so you need to specify it by using the properties mentioned above.
Providing Fixed Ports
你还可以使用固定端口运行 Stub。你可以采用两种不同的方式实现。一种是将其传递到属性中,另一种是使用 JUnit 规则的 Fluent API。
You can also run your stubs on fixed ports. You can do it in two different ways. One is to pass it in the properties, and the other is to use the fluent API of JUnit rule.
Fluent API
当使用 StubRunnerRule
或 StubRunnerExtension
时,你可以添加要下载的 Stub,然后传递最后一个下载的 Stub 的端口。以下示例展示了如何执行此操作:
When using the StubRunnerRule
or StubRunnerExtension
, you can add a stub to download
and then pass the port for the last downloaded stub. The following example shows how to do so:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit4/StubRunnerRuleCustomPortJUnitTest.java[]
对于前面的示例,以下测试有效:
For the preceding example, the following test is valid:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/junit4/StubRunnerRuleCustomPortJUnitTest.java[]
Stub Runner with Spring
带 Spring 的 Stub Runner 会设置 Stub Runner 项目的 Spring 配置。
Stub Runner with Spring sets up Spring configuration of the Stub Runner project.
通过在配置文件中提供一个 Stub 列表,Stub Runner 会自动下载并 WireMock 中注册选定的 Stub。
By providing a list of stubs inside your configuration file, Stub Runner automatically downloads and registers in WireMock the selected stubs.
如果你希望查找 Stub 依赖项的 URL,你可以自动装入 StubFinder
接口并使用它的方法,如下所示:
If you want to find the URL of your stubbed dependency, you can autowire the StubFinder
interface and use
its methods, as follows:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/StubRunnerConfigurationSpec.groovy[]
这样做取决于以下配置文件:
Doing so depends on the following configuration file:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/resources/application-test.yml[]
除了使用属性,你还可以使用 @AutoConfigureStubRunner
中的属性。以下示例通过在注释中设置值来实现相同的结果:
Instead of using the properties, you can also use the properties inside the @AutoConfigureStubRunner
.
The following example achieves the same result by setting values on the annotation:
Unresolved directive in stub-runner-junit.adoc - include::{stubrunner_core_path}/src/test/groovy/org/springframework/cloud/contract/stubrunner/spring/cloud/consul/StubRunnerSpringCloudConsulAutoConfigurationSpec.groovy[]
Stub Runner Spring 以下述方式为每个已注册的 WireMock 服务器注册环境变量。以下示例展示了 com.example:thing1
和 com.example:thing2
的 Stub Runner ID:
Stub Runner Spring registers environment variables in the following manner
for every registered WireMock server. The following example shows Stub Runner IDs for
com.example:thing1
and com.example:thing2
:
-
stubrunner.runningstubs.thing1.port
-
stubrunner.runningstubs.com.example.thing1.port
-
stubrunner.runningstubs.thing2.port
-
stubrunner.runningstubs.com.example.thing2.port
你可以在你的代码中引用这些值。
You can reference these values in your code.
你还可以使用 @StubRunnerPort
注释来注入正在运行的 Stub 的端口。此注释的值可以是 groupid:artifactid
或者仅是 artifactid
。以下示例展示了 com.example:thing1
和 com.example:thing2
的 Stub Runner ID。
You can also use the @StubRunnerPort
annotation to inject the port of a running stub.
The value of the annotation can be the groupid:artifactid
or only the artifactid
.
The following example works shows Stub Runner IDs for
com.example:thing1
and com.example:thing2
.
@StubRunnerPort("thing1")
int thing1Port;
@StubRunnerPort("com.example:thing2")
int thing2Port;