request {
+ url '/foo'
+ method GET()
+}
+response {
+ status 200
+ body(
+ foo: "foo"
+ }
+}
+From 768a0e938aa43cb331c3b171bdb1de60eced0341 Mon Sep 17 00:00:00 2001
From: buildmaster
There are cases in which 2 consumers of the same endpoint want to have 2 different responses.
+|
+ Tip
+ |
++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. + | +
Let’s look at the following example for contract defined for the producer called producer.
+There are 2 consumers: foo-consumer and bar-consumer.
Consumer foo-service
request {
+ url '/foo'
+ method GET()
+}
+response {
+ status 200
+ body(
+ foo: "foo"
+ }
+}
+Consumer bar-service
request {
+ url '/foo'
+ method GET()
+}
+response {
+ status 200
+ body(
+ bar: "bar"
+ }
+}
+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 stubsPerConsumer feature.
On the producer side the consumers can have a folder that contains contracts related only to them.
+By setting the stubrunner.stubs-per-consumer flag to true 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.
On the foo producer side the contracts would look like this
.
+└── contracts
+ ├── bar-consumer
+ │ ├── bookReturnedForBar.groovy
+ │ └── shouldCallBar.groovy
+ └── foo-consumer
+ ├── bookReturnedForFoo.groovy
+ └── shouldCallFoo.groovy
+Being the bar-consumer consumer you can either set the spring.application.name or the stubrunner.consumer-name to bar-consumer
+Or set the test as follows:
@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 {
+...
+}
+Then only the stubs registered under a path that contains the bar-consumer in its name (i.e. those from the
+src/test/resources/contracts/bar-consumer/some/contracts/… folder) will be allowed to be referenced.
Or set the consumer name explicitly
+@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 {
+...
+}
+Then only the stubs registered under a path that contains the foo-consumer in its name (i.e. those from the
+src/test/resources/contracts/foo-consumer/some/contracts/… folder) will be allowed to be referenced.
You can check out issue 224 for more +information about the reasons behind this change.
+