Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Resilience testing with Wiremock and Spock

2,950 views

Published on

Short tutorial for using Wiremock in your unit tests.
Links to https://github.com/koenighotze/wiremock-tryout

Published in: Technology
  • I didn't think failing my maths back in high school would come back to haunt me but it did! After graduating from Edge Hill University, I was looking to undertake a PGCE in primary teaching. However, one of the requirements was a pass in GCSE maths so I had no choice but to re-take it. After failing it twice, my confidence was very low and I didn't think I could qualify for the PGCE. After running a Google search on passing GCSE maths, I came across Jeevan's revision system. The feedback it received looked very good so I decided to give it a try... And it was one of the best decisions I've ever made. After going through Jeevan's guide, I managed to grasp the entire subject and I passed my next GCSE maths exam, with ease. He (Jeevan) provided me with all the tools I needed to prepare for the exam. Together with his guidance, my pass was a foregone conclusion. Thank you so much Jeevan! You have potentially changed my life. I recommend anyone seeking a pass grade in GCSE maths to purchase this fantastic package!▲▲▲ http://t.cn/AirrSv7D
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here

Resilience testing with Wiremock and Spock

  1. 1. <david.schmitz@senacor.com> Resilient Services with Wiremock Effective REST tests using Wiremock and Spock https://flic.kr/p/orejhX
  2. 2. <david.schmitz@senacor.com> How does your system handle failures?
  3. 3. <david.schmitz@senacor.com> Everybody tests the happy path… https://flic.kr/p/9MHEme
  4. 4. <david.schmitz@senacor.com> Happy Path Tests HTTP/1.1 200 OK {“publicId":"42634558","data":"foo"} GET http://service/resource/12 Client Service
  5. 5. <david.schmitz@senacor.com> But what about the not-so-happy path? https://flic.kr/p/CSnr1
  6. 6. <david.schmitz@senacor.com> Enter Wiremock • Stub and Mock framework for testing HTTP(s) traffic • Features include • Request verification, • record, playback, • fault injection, • unit test and stand alone Client Unit Test Embedded Wiremock Client Standalone Wiremock
  7. 7. <david.schmitz@senacor.com> How does it work? Client Wiremock Request Mapping Response Data $ curl -H ‘Accept: application/json’ http://localhost:8080/sample/
  8. 8. <david.schmitz@senacor.com> How does it work? Client Wiremock Request Mapping Response Data • Starts embedded Jetty Server • Acts as a proxy to the actual service • Is used by the client transparently
  9. 9. <david.schmitz@senacor.com> How does it work? Client Wiremock Request Mapping Response Data "request" : { "url" : "/sample/", "method" : "GET", } } }, "response" : { "status" : 200, "bodyFileName" : "body.json", "headers" : { "Content-Type" : “application/json" … } }
  10. 10. <david.schmitz@senacor.com> How does it work? Client Wiremock Request Mapping Response Data body.json: [{"publicId":"...","data":"..."}, {"publicId":"...","data":"..."}]
  11. 11. <david.schmitz@senacor.com> Creating a Wiremock Unittest Prepare Stub Configure Wiremock Consume REST service
  12. 12. <david.schmitz@senacor.com> Creating a Wiremock Unittest Prepare Stub Configure Wiremock Consume REST service
  13. 13. <david.schmitz@senacor.com> Creating a Wiremock Unittest Prepare Stub Configure Wiremock Consume REST service
  14. 14. <david.schmitz@senacor.com> Creating a Wiremock Unittest Prepare Stub Configure Wiremock Consume REST service
  15. 15. <david.schmitz@senacor.com> Creating a Wiremock Unittest Prepare Stub Configure Wiremock Consume REST service
  16. 16. <david.schmitz@senacor.com> …but what about failures? HTTP/1.1 503 Service Unavailable Client Service GET http://service/resource/12
  17. 17. <david.schmitz@senacor.com> Testing Failures
  18. 18. <david.schmitz@senacor.com> …but what about bad responses? Client Service HTTP/1.1 200 OK Content-Type: application/json;charset=UTF-8 [300 MB of random garbage] GET http://service/resource/12
  19. 19. <david.schmitz@senacor.com> Testing Unexpected Behavior
  20. 20. <david.schmitz@senacor.com> …but what about timeouts? java.net.SocketTimeoutException: Read timed out Client Service GET http://service/resource/12
  21. 21. <david.schmitz@senacor.com> Testing the circuit breaker
  22. 22. Record and Playback https://flic.kr/p/nRXLqh
  23. 23. <david.schmitz@senacor.com> Record and Playback Run requests Start Wiremock in record mode Start Wiremock in playback mode
  24. 24. <david.schmitz@senacor.com> Record and Playback Demo
  25. 25. <david.schmitz@senacor.com> Summary • Resilience can be tested • Do not rely on happy paths and mocks only • Use stubs and verify in unit tests • Use record and playback for external services • Make sure not to be the developer responsible for a downtime during the busy-business-time :D
  26. 26. <david.schmitz@senacor.com> Links http://wiremock.org/ https://github.com/koenighotze/wiremock-tryout/
  27. 27. <david.schmitz@senacor.com> THANK YOU! <david.schmitz@senacor.com>
  28. 28. <david.schmitz@senacor.com> BACKUP
  29. 29. <david.schmitz@senacor.com> Testing a Client Client Unit Test Embedded Wiremock

×