Tuesday, March 14, 2017

Consumer Driven Testing with Pact & Spring Boot

Recently a colleague of mine stumbled across Pact.io,  Our current application had grown to over 50 services and we we're starting to have some integration test failures and a brittle dev / acceptance test environment. So we decided to have a look at ways to try help with this.

I started out by reading:
https://docs.pact.io/faq/convinceme.html

Then watching:
https://www.youtube.com/watch?v=-6x6XBDf9sQ&feature=youtu.be

Those 2 resources convinced me to give it a shot.

So I set out and created a quick set of Spring boot apps, the GitHub repo here, to test out the concepts and get everything working.

To highlight some important bits from the demo.

Consumer:
As Pact is a consumer driven test framework. This is where you define a unit test, that test mocks the http server response and you assert against that.
Once the test is successful it creates a pact json file in the /pacts directory.

So after the "mock" test is run and the pact file has been created. You need to include a maven plugin ...pact... that is then used to publish the content of the pacts/ folder to the pact broker... which is defined in the pom as below.

Producer:

This uses the JUnit integration from Pact.io to download the pacts from the broker and then run against an running service.

Since this already uses a @RunWith annotation, I could not use the spring boot runner. So to get around that as a before class step, I start the Spring boot application, the pacts then gets run against that running instance... and the boot application gets stopped again after the tests. Depending on your use case I guess it would also be an option to do this with @Before so you get a new service instance started before each pack, but that would slow down the execution tremendously.

The @State annotation, allows for clients to define a specific state, which the producer can the use to setup additional data / conditions required for the test to run.

Once the pacts have executed against the service there are reports generated in the target folder.




Setting up the Pact Broker

1. Grab the public images from Docker Hub.
docker pull dius/pact_broker
docker pull postgres

2. Then setup the Postgres DB
docker run --name pactbroker-db -e POSTGRES_PASSWORD=ThePostgresPassword -e POSTGRES_USER=admin -d postgres
docker run -it --link pactbroker-db:postgres --rm postgres psql -h postgres -U admin
CREATE USER pactbrokeruser WITH PASSWORD 'TheUserPassword';
CREATE DATABASE pactbroker WITH OWNER pactbrokeruser;
GRANT ALL PRIVILEGES ON DATABASE pactbroker TO pactbrokeruser;
3. Once the DB is up, run the actual Broker:
docker run --name pactbroker --link pactbroker-db:postgres -e PACT_BROKER_DATABASE_USERNAME=pactbrokeruser -e PACT_BROKER_DATABASE_PASSWORD=TheUserPassword -e PACT_BROKER_DATABASE_HOST=postgres -e PACT_BROKER_DATABASE_NAME=pactbroker -d -p 80:80 dius/pact_broker


Extra References:

https://docs.pact.io/documentation/
https://docs.pact.io/documentation/sharings_pacts.html
https://github.com/DiUS/pact-jvm
https://github.com/DiUS/pact-jvm/tree/master/pact-jvm-consumer-junit

Get the example project
https://github.com/bdupreez/pactdemo


24 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

  2. When it comes to gaming consoles, there is always a big fight between Sony's Playstation and Microsoft's XBox. Now this rivalry is going to increase even more because Microsoft is getting ready to launch it's next console, the Xbox 2. You can check out all the details from my site here Xbox 2 Price

    ReplyDelete
  3. Hi, I was trying to generate a pact contract ( pact-jvm) using this example as a reference. I have a question- should the builder.body() be exactly same as the forEntity and expectedResponse in the below statement?
    assertThat(forEntity, sameBeanAs(expectedResponse));

    ReplyDelete
  4. Thank for your post. It's very helpful.
    By the way, follow my website to get tips about gmail login

    ReplyDelete
  5. In the 20th century, the textile industry in the developing countries like China and India and those located in the Central America emerged as powerful competitor to the industry in developed nations. digital bursting strength tester

    ReplyDelete
  6. Well written, you give here a lot of valuable tips.

    ReplyDelete
  7. I did ennjoyed reading this content

    ReplyDelete
  8. I very like to read such posts, they are really interesting.

    ReplyDelete
  9. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Faribault Website Design

    ReplyDelete
  10. it was a wonderful chance to visit this kind of site and I am happy to know. thank you so much for giving us a chance to have this opportunity.. Penetration Testing Provider

    ReplyDelete
  11. This is a brilliant blog! I'm very happy with the comments!.. learn more

    ReplyDelete
  12. This machine guarantees acceptable consistence to modern wellbeing guidelines and assembling rehearses. tensile strength testing machine

    ReplyDelete
  13. Good website! I truly love how it is easy on my eyes it is. I am wondering how I might be notified whenever a new post has been made. convert pdf to word

    ReplyDelete
  14. How is emergency care tested for STDs? The testing procedure at the Emergency Care Center is similar to what is done at other clinics and medical offices. A medical provider will examine the patient to see if he or she is sexually active and based on the results. Blood or urine samples, as well as swabs from different parts of the body may be drawn. If symptoms indicate an STD is present, the patient will be asked to return to the clinic for further testing. For women, an examination of the pelvic area may be required. Here is information about test for STDs, read on for more information.

    ReplyDelete
  15. If you are asking if is actual if the Neco exam Questions paper leaks? Or does the Neco exam Questions paper leak for 2023? Or do the Neco examination goal and theory Questions paper leak? The answer is sure, Neco exam Questions do Leak. Sure, you'll be amazed to pay attention us say Neco exam Questions do leak. Here's information about how to get waec gce 2023 expo, extra info study on to recognise.

    ReplyDelete
  16. Unlock the future of technology. With Futurengage White Label Crypto Exchange Software Development, you can expect a secure, customized, and scalable solution, tailored to your needs. Launch your platform right away for the most incredible result.

    ReplyDelete
  17. SK Corporation's RO water treatment solution in bangladesh offer certified, efficient filtration with various capacities. Their brand reputation, after-sales service, and warranty ensure clean, pure water while being budget-friendly and reliable.

    ReplyDelete
  18. Unveil the secrets of Bangladesh's allure with "Bangladesh Scenic Tours." Immerse yourself in the vibrancy of Dhaka, traverse the tranquil riverine landscapes, and explore the archaeological wonders of Bishnupur. Their thoughtfully crafted tours promise an intimate encounter with the diverse facets of Beautiful Bangladesh, creating lasting memories of a truly enriching journey Tour Operator in Bangladesh.

    ReplyDelete
  19. As the demand for AWS professionals continues to soar, choosing the right training institute is pivotal to your success. APTRON Solutions in Noida stands as a beacon of excellence, empowering individuals to master AWS and thrive in the cloud computing era. Elevate your career with our industry-centric AWS Training Institute in Noida and pave the way for a promising future in the world of cloud technology.

    ReplyDelete
  20. Clearly, it's an enticing article for us which you've got provided here about the driven testing and mystery of Code U231748506 This is often an excellent resource to reinforce knowledge about it. Thank you.

    ReplyDelete
  21. 使用減肥藥網購時,選擇信譽良好的品牌,核實成分,閱讀評論。避免使用承諾不切實際的產品,並諮詢醫療保健專業人員。確保網站安全、經過認證,並提供退款保證。

    ReplyDelete
  22. Maximize your earnings with Crypto Wallet using your wallet. Explore trading, staking, mining, airdrops, DeFi, NFTs, and freelancing. Stay secure, diversify investments, and embrace innovative opportunities for passive income and long-term financial success.

    ReplyDelete

Popular Posts

Followers