Testing interdependent projects with Spring Cloud Contract
using:
- maven 3.5.2
- spring-cloud and spring-cloud-contract version 2.0.0.RC1;
with projects:
- spring-cloud-starter-stream-kafka
- spring-cloud-stream-test-support
- spring-cloud-starter-contract-verifier
- spring-cloud-starter-contract-stub-runner
I am developing an app using microservices architecture, with event-sourcing through Kafka. My project is structured like this:
example-app
|-- pom.xml (sets up both subprojects as children)
|-- client-service (handles person's money)
| |-- src/main/java/client/EventProducer.java (produces a clientCreated event)
| `-- src/main/java/client/EventConsumer.java (consumes a productBought event)
`-- product-service (handles products that can be bought)
|-- src/main/java/product/EventProducer.java (produces a productBought event)
`-- src/main/java/product/EventConsumer.java (consumes a clientCreated event)
With Event Sourcing, both my services are producers and consumers, with client-service producing a clientCreated event consumed by product-service, and my product-service producing a productBought event consumed by client-service. In this scenario, both services generate stubs for the other to consume, as such:
client-service integration tests
@SpringBootTest(classes = ClientApplication.class, webEnvironment = RANDOM_PORT)
@AutoConfigureStubRunner(ids = "example.com:product-service", stubsMode = LOCAL)
class PersonIntegrationTests extends Specification {
@Inject StubTrigger stubTrigger
// tests
}
product-service integration tests
@SpringBootTest(classes = ProductApplication.class, webEnvironment = RANDOM_PORT)
@AutoConfigureStubRunner(ids = "com.example:client-service", stubsMode = LOCAL)
class ProductIntegrationTests extends Specification {
@Inject StubTrigger stubTrigger
// tests
}
Now, of course, running mvn clean install in the parent project gives me an error because regardless of the build order, the stubs in the other project have not yet been generated. It's a chicken and egg problem.
I've also tried setting up an external contract project (following the beer-contracts and producer-external-contract sample), but the stubs are still generated by each project.
As a workaround, I'm running mvn clean install -DskipTests first to generate the stubs, then mvn clean install to test the entire app. I imagine in gradle it would be very simple to generate the stubs for all projects first before actually running the tests, but maven is not so configurable.
I would like, if possible, to be able to generate the stubs before all the tests.
As a suggestion, maybe there could be some sort of "external stubs" option that would be placed first in the build order and centralize all the stubs in one place, so I could configure each test using @AutoConfigureStubRunner(ids = "example.com:stubs-service"). In the spring-cloud-contract-maven-plugin of this external stubs's pom.xml, I'd list every producer in my project, so that it could generate the stubs.
Hi!
Just a few cents from my side:
I am not saying it is definitely wrong, but have you thought about boundaries of your system and why they need two-way communications? Have you considered that maybe those 2 are just in facts one service?
I can be rarely seen that two systems are downstream and upstream at the same time to each other. Although there are situations where we need two-way communication via messaging, for instance when you want to implement request-reply with messaging (https://www.enterpriseintegrationpatterns.com/patterns/messaging/RequestReply.html)
Are you sure you use event sourcing? Or just messaging with events? Event Sourcing is a pattern when you don't persist state - just events from which you rebuild state.
@pilloPl interesting observations... Well, I've just started working with Event Sourcing, and I'm actually doing both, saving state (to quickly get the most recent state), but also saving the events so I can rebuild them (in case I start a new service with its own fresh database). As for the boundaries of the system, the real application I'm developing is more complex that the example I gave. I actually have many microservices interacting with each other, I've separated them by domain, and they communicate through the Saga pattern.
It's actually something I've been worrying about, whether my system is actually a distributed system or just a distributed monolith, whether there's actually too much coupling between them. I honestly don't know yet.
Closing due to age of the question. If you would like us to look at this issue, please comment and we will look at re-opening the issue.