An example of how to apply BDD in a microservice architecture using consumer driven contracts
This project is an example of how to apply BDD to the development of an application based on microservices using Consumer
Driven Contract Testing.
You can get more detail about it in the following articles:
The application allows us to browse a product catalogue and to get the details and available offers for
each of the products in the catalogue.
The application is divided in the following components:
As example following three use scenarios have been implemented end to end, starting in the frontend application and ending
in the two backend microservices.
It uses Cucumber as BDD framework in the frontend application and Pact as
Consumer Driven Contract framework to translate the requirements for the different scenarios into consumer-provider contracts
that the different microservices must satisfy in order to consider the scenarios fully implemented.
The project is built using gradle and it has four subprojects, one for each of the components. This is made so for demonstrative
purposes, as in a real world application each of this components would have its own code base and would be owned and developed
by a different team, and the build process would be more decoupled as is here.
Docker compose is used to run a Pact Broker used to
share the pacts between the different services during the build process. Another docker compose application configuration
file can be used to start all the services that compose the application so we can see is in action.
In order to build and run the demo application you should have previously installed the following requirements:
To be able to build the application docker has to be running in your computer, so that the pact broker can be started.
To build and start the demo application go to the project’s root directory and run:
# pull the docker images required by the pact broker to avoid timeouts during the application build
docker pull postgres
docker pull dius/pact-broker
# build the application and start it
./gradlew bddPactAppComposeUp
Now you can relax and wait until it has finished, which can take some minutes depending on your Internet connection.
To have an idea of how is the build process, this command will execute the following tasks:
After it has successfully finished we’ll be able to access the application in http://localhost:4200.
We can also access the different contracts between the services accessing the Pact Broker in http://localhost:1080.