Sync docs from master to gh-pages
This commit is contained in:
@@ -684,6 +684,10 @@ $(addBlockSwitches);
|
||||
</li>
|
||||
<li><a href="#_example">Example</a></li>
|
||||
<li><a href="#_stub_runner_boot_with_service_discovery">Stub Runner Boot with Service Discovery</a></li>
|
||||
</ul>
|
||||
</li>
|
||||
<li><a href="#_stubs_per_consumer">Stubs Per Consumer</a>
|
||||
<ul class="sectlevel3">
|
||||
<li><a href="#_common_properties_for_junit_and_spring">Common properties for JUnit and Spring</a>
|
||||
<ul class="sectlevel4">
|
||||
<li><a href="#_stub_runner_stubs_ids">Stub runner stubs ids</a></li>
|
||||
@@ -5181,6 +5185,135 @@ likely to change. That way you can provide only the list of stubs to download wh
|
||||
the Stub Runner Boot.</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="sect2">
|
||||
<h3 id="_stubs_per_consumer">Stubs Per Consumer</h3>
|
||||
<div class="paragraph">
|
||||
<p>There are cases in which 2 consumers of the same endpoint want to have 2 different responses.</p>
|
||||
</div>
|
||||
<div class="admonitionblock tip">
|
||||
<table>
|
||||
<tr>
|
||||
<td class="icon">
|
||||
<div class="title">Tip</div>
|
||||
</td>
|
||||
<td class="content">
|
||||
This approach also allows you to immediately know which consumer is using which part of your API.
|
||||
You can remove part of a response that your API produces and you can see which of your autogenerated tests
|
||||
fails. If none fails then you can safely delete that part of the response cause nobody is using it.
|
||||
</td>
|
||||
</tr>
|
||||
</table>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Let’s look at the following example for contract defined for the producer called <code>producer</code>.
|
||||
There are 2 consumers: <code>foo-consumer</code> and <code>bar-consumer</code>.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p><strong>Consumer <code>foo-service</code></strong></p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-groovy" data-lang="groovy">request {
|
||||
url '/foo'
|
||||
method GET()
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
body(
|
||||
foo: "foo"
|
||||
}
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p><strong>Consumer <code>bar-service</code></strong></p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-groovy" data-lang="groovy">request {
|
||||
url '/foo'
|
||||
method GET()
|
||||
}
|
||||
response {
|
||||
status 200
|
||||
body(
|
||||
bar: "bar"
|
||||
}
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>You can’t produce for the same request 2 different responses. That’s why you can properly package the
|
||||
contracts and then profit from the <code>stubsPerConsumer</code> feature.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>On the producer side the consumers can have a folder that contains contracts related only to them.
|
||||
By setting the <code>stubrunner.stubs-per-consumer</code> flag to <code>true</code> we no longer register all stubs but only those that
|
||||
correspond to the consumer application’s name. In other words we’ll scan the path of every stub and
|
||||
if it contains the subfolder with name of the consumer in the path only then will it get registered.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>On the <code>foo</code> producer side the contracts would look like this</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-bash" data-lang="bash">.
|
||||
└── contracts
|
||||
├── bar-consumer
|
||||
│ ├── bookReturnedForBar.groovy
|
||||
│ └── shouldCallBar.groovy
|
||||
└── foo-consumer
|
||||
├── bookReturnedForFoo.groovy
|
||||
└── shouldCallFoo.groovy</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Being the <code>bar-consumer</code> consumer you can either set the <code>spring.application.name</code> or the <code>stubrunner.consumer-name</code> to <code>bar-consumer</code>
|
||||
Or set the test as follows:</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-groovy" data-lang="groovy">@ContextConfiguration(classes = Config, loader = SpringBootContextLoader)
|
||||
@SpringBootTest(properties = ["spring.application.name=bar-consumer"])
|
||||
@AutoConfigureStubRunner(ids = "org.springframework.cloud.contract.verifier.stubs:producerWithMultipleConsumers",
|
||||
repositoryRoot = "classpath:m2repo/repository/",
|
||||
stubsPerConsumer = true)
|
||||
@DirtiesContext
|
||||
class StubRunnerStubsPerConsumerSpec extends Specification {
|
||||
...
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Then only the stubs registered under a path that contains the <code>bar-consumer</code> in its name (i.e. those from the
|
||||
<code>src/test/resources/contracts/bar-consumer/some/contracts/…​</code> folder) will be allowed to be referenced.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Or set the consumer name explicitly</p>
|
||||
</div>
|
||||
<div class="listingblock">
|
||||
<div class="content">
|
||||
<pre class="highlight"><code class="language-groovy" data-lang="groovy">@ContextConfiguration(classes = Config, loader = SpringBootContextLoader)
|
||||
@SpringBootTest
|
||||
@AutoConfigureStubRunner(ids = "org.springframework.cloud.contract.verifier.stubs:producerWithMultipleConsumers",
|
||||
repositoryRoot = "classpath:m2repo/repository/",
|
||||
consumerName = "foo-consumer",
|
||||
stubsPerConsumer = true)
|
||||
@DirtiesContext
|
||||
class StubRunnerStubsPerConsumerWithConsumerNameSpec extends Specification {
|
||||
...
|
||||
}</code></pre>
|
||||
</div>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>Then only the stubs registered under a path that contains the <code>foo-consumer</code> in its name (i.e. those from the
|
||||
<code>src/test/resources/contracts/foo-consumer/some/contracts/…​</code> folder) will be allowed to be referenced.</p>
|
||||
</div>
|
||||
<div class="paragraph">
|
||||
<p>You can check out <a href="https://github.com/spring-cloud/spring-cloud-contract/issues/224">issue 224</a> for more
|
||||
information about the reasons behind this change.</p>
|
||||
</div>
|
||||
<div class="sect3">
|
||||
<h4 id="_common_properties_for_junit_and_spring">Common properties for JUnit and Spring</h4>
|
||||
<div class="paragraph">
|
||||
|
||||
Reference in New Issue
Block a user