resilience testing with wiremock and spock

29
<[email protected]> Resilient Services with Wiremock Effective REST tests using Wiremock and Spock https://flic.kr/p/orejhX

Upload: david-schmitz

Post on 08-Jan-2017

717 views

Category:

Technology


5 download

TRANSCRIPT

Page 1: Resilience testing with Wiremock and Spock

<[email protected]>

Resilient Services with Wiremock Effective REST tests using Wiremock and Spock

https://flic.kr/p/orejhX

Page 2: Resilience testing with Wiremock and Spock

<[email protected]>

How does your system handle failures?

Page 3: Resilience testing with Wiremock and Spock

<[email protected]>

Everybody tests the happy path…

https://flic.kr/p/9MHEme

Page 4: Resilience testing with Wiremock and Spock

<[email protected]>

Happy Path Tests

HTTP/1.1 200 OK

{“publicId":"42634558","data":"foo"}

GET http://service/resource/12

Client Service

Page 5: Resilience testing with Wiremock and Spock

<[email protected]>

But what about the not-so-happy path?

https://flic.kr/p/CSnr1

Page 6: Resilience testing with Wiremock and Spock

<[email protected]>

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

Page 7: Resilience testing with Wiremock and Spock

<[email protected]>

How does it work?

Client Wiremock

Request Mapping

Response Data

$ curl -H ‘Accept: application/json’ http://localhost:8080/sample/

Page 8: Resilience testing with Wiremock and Spock

<[email protected]>

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

Page 9: Resilience testing with Wiremock and Spock

<[email protected]>

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" … } }

Page 10: Resilience testing with Wiremock and Spock

<[email protected]>

How does it work?

Client Wiremock

Request Mapping

Response Data

body.json: [{"publicId":"...","data":"..."}, {"publicId":"...","data":"..."}]

Page 11: Resilience testing with Wiremock and Spock

<[email protected]>

Creating a Wiremock Unittest

Prepare Stub

Configure Wiremock

Consume REST service

Page 12: Resilience testing with Wiremock and Spock

<[email protected]>

Creating a Wiremock Unittest

Prepare Stub

Configure Wiremock

Consume REST service

Page 13: Resilience testing with Wiremock and Spock

<[email protected]>

Creating a Wiremock Unittest

Prepare Stub

Configure Wiremock

Consume REST service

Page 14: Resilience testing with Wiremock and Spock

<[email protected]>

Creating a Wiremock Unittest

Prepare Stub

Configure Wiremock

Consume REST service

Page 15: Resilience testing with Wiremock and Spock

<[email protected]>

Creating a Wiremock Unittest

Prepare Stub

Configure Wiremock

Consume REST service

Page 16: Resilience testing with Wiremock and Spock

<[email protected]>

…but what about failures?

HTTP/1.1 503 Service Unavailable

Client Service

GET http://service/resource/12

Page 18: Resilience testing with Wiremock and Spock

<[email protected]>

…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

Page 19: Resilience testing with Wiremock and Spock

<[email protected]>

Testing Unexpected Behavior

Page 20: Resilience testing with Wiremock and Spock

<[email protected]>

…but what about timeouts?

java.net.SocketTimeoutException: Read timed out

Client Service

GET http://service/resource/12

Page 21: Resilience testing with Wiremock and Spock

<[email protected]>

Testing the circuit breaker

Page 22: Resilience testing with Wiremock and Spock

Record and Playback

https://flic.kr/p/nRXLqh

Page 23: Resilience testing with Wiremock and Spock

<[email protected]>

Record and Playback

Run requests

Start Wiremock in record mode

Start Wiremock in playback mode

Page 24: Resilience testing with Wiremock and Spock

<[email protected]>

Record and Playback

Demo

Page 25: Resilience testing with Wiremock and Spock

<[email protected]>

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

Page 26: Resilience testing with Wiremock and Spock

<[email protected]>

Links

http://wiremock.org/

https://github.com/koenighotze/wiremock-tryout/

Page 29: Resilience testing with Wiremock and Spock

<[email protected]>

Testing a ClientClient Unit Test

EmbeddedWiremock