resilience testing with wiremock and spock
TRANSCRIPT
Resilient Services with Wiremock Effective REST tests using Wiremock and Spock
https://flic.kr/p/orejhX
Everybody tests the happy path…
https://flic.kr/p/9MHEme
Happy Path Tests
HTTP/1.1 200 OK
{“publicId":"42634558","data":"foo"}
GET http://service/resource/12
Client Service
But what about the not-so-happy path?
https://flic.kr/p/CSnr1
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
EmbeddedWiremock
Client Standalone Wiremock
How does it work?
Client Wiremock
Request Mapping
Response Data
$ curl -H ‘Accept: application/json’ http://localhost:8080/sample/
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
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" … } }
How does it work?
Client Wiremock
Request Mapping
Response Data
body.json: [{"publicId":"...","data":"..."}, {"publicId":"...","data":"..."}]
Creating a Wiremock Unittest
Prepare Stub
Configure Wiremock
Consume REST service
Creating a Wiremock Unittest
Prepare Stub
Configure Wiremock
Consume REST service
Creating a Wiremock Unittest
Prepare Stub
Configure Wiremock
Consume REST service
Creating a Wiremock Unittest
Prepare Stub
Configure Wiremock
Consume REST service
Creating a Wiremock Unittest
Prepare Stub
Configure Wiremock
Consume REST service
…but what about failures?
HTTP/1.1 503 Service Unavailable
Client Service
GET http://service/resource/12
…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
…but what about timeouts?
java.net.SocketTimeoutException: Read timed out
Client Service
GET http://service/resource/12
Record and Playback
https://flic.kr/p/nRXLqh
Record and Playback
Run requests
Start Wiremock in record mode
Start Wiremock in playback mode
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
Links
http://wiremock.org/
https://github.com/koenighotze/wiremock-tryout/
THANK YOU!<[email protected]>
Testing a ClientClient Unit Test
EmbeddedWiremock