spectolabs xebia testworks conf october 2016
TRANSCRIPT
Automated Testing in a World of Interdependence:Service Virtualisation for the Modern Age
TestWorksConf Workshop, 7th October
Benji Hooper, Daniel Bryant, John Davenport, Tom Cunliffe
About Specto Workshops
• This slide deck is an excerpt from a workshop delivered as part of the test automation TestWorksConf
• http://testworksconf.com/talk/automated-testing-in-a-world-of-interdependence
• The Specto team run public and private workshops ([email protected])
• All aspects of test automation
• Microservice testing and service virtualisation with Hoverfly
• Incorporating FR / NFR testing into a build pipeline
Knight Capital, 2012
$440m loss
Neiman Marcus, Black Friday 2015
IPO cancelled
Stock trading disruption
12 hour outage
Royal Bank of Scotland, 2012 Transactions disrupted worldwide£500m in fines and
compensation
In any complex distributed system…
… testing an individual component in isolation is difficult….
… and test automation is sometimes impossible
Agenda
• Group flip-chart sessions (30 minutes)
• Introduction to service virtualization (20 minutes)
• Hands-on session (50 minutes)
• Wrap up
Group flip-chart session
• Equal-sized groups:
• 1 facilitator - Edze, Tom, Daniel, Benji and John
• 1 scribe
• 1 presenter
• 30 minutes
• 10-15 minutes: read the scenario & discuss issues
• 4 presentations of 4 minutes - your main issues and solutions
credit to @mogronalol 16
Non Deterministic
assertThat(booking.getDate()) .isEqualTo(LocalDate.of(2013,1,8));
Consumer
Booking Service
Consumer
Booking Service
Consumer
Booking Service {
“date” : “2013-01-08”, … }
{
“date” : “2016-02-07”, … }
credit to @mogronalol 17
Fault Injection
Consumer
Producer
Consumer
Producer
4xx, 5xx, timeouts
Operationally challenging to trigger
faults
credit to @mogronalol 18
Service Virtualisation
• Emulation of real service • Non-intrusive • Lightweight - or should be
credit to @mogronalol 21
Creating Data
https://github.com/tomakehurst/wiremock
credit to @mogronalol 24
Not Mocking
Service Under Test
Mocking
Dependant Service
Http
Virtual Service
Mocks bleed into the architectural boundary of
your application
credit to @mogronalol 27
JUnit Example
@Rule public HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode(“src/test/resources/external.json").build();
@Testpublic void shouldBeAbleToRetrieveABooking() throws URISyntaxException { // Given final int bookingId = 1; // When final Booking booking = bookingServiceGateway.getBooking(bookingId); // Then assertThat(booking.getCustomer()).isEqualTo(“Andrew Morgan”); assertThat(booking.getDate()).isEqualTo(LocalDate.of(2016,3,1));}
credit to @mogronalol 28
JUnit Example{ "data": [ { "request": { "path": "/api/bookings/1", "method": "GET", "destination": "www.my-test.com", "query": null, "body": "" }, "response": { "status": 200, "body": “{\”customer\”:\”Andrew Morgan\", \”date\”: \”2016-03-01\”}}}”, "headers": { "Content-Type": [ "application/json" ] } } } ]}
credit to @mogronalol 29
JUnit Example
@Testpublic void shouldBeAbleToRetrieveABooking() throws URISyntaxException { // Given final int bookingId = 1; // When final Booking booking = bookingServiceGateway.getBooking(bookingId); // Then assertThat(booking.getCustomer()).isEqualTo(“Andrew Morgan”); assertThat(booking.getDate()).isEqualTo(LocalDate.of(2016,3,1));}
@Rule public HoverflyRule hoverflyRule = HoverflyRule.buildFromClassPathResource(“external.json").build();
credit to @mogronalol 30
Advantages
• Isolation • Flaky dependencies • Rate limiting • Deterministic • Licensing
credit to @mogronalol 31
Deployment and Infrastructure
Consumer External Producer
Virtual Service
Internal Producer
Internal Producer
Internal Producer
Virtual Service
Virtual Service
Virtual Service
credit to @mogronalol 32
CLI Example
$ hoverctl start —proxy-port 8080 $ hoverctl import accounts-service-simulation.json
$ hoverctl start —proxy-port 8081 $ hoverctl import payments-service-simulation.json
$ curl http://www.account-service.com/api/v1/accounts/1 —proxy http://localhost:8080
[{"name":"foo"}]
$ curl http://www.payment-service.com/api/v1/payments/1 -proxy http://localhost:8081
[{“amount":"£23.27"}]
Hands-on session
• Exercise 1: Explore an application that needs testing and create the simulations required to test it independently
• Exercise 2: Test the circuit breakers in an application using Hoverfly
• Code available:
• https://github.com/benjih/oreilly-docker-java-shopping
• Edze, Tom, Daniel, Benji and John will be around for help if needed (and feel free to work with others!)
A simulation of a component behaves like the real thing…
… it misbehaves like the real thing…
So you can create a realistic, repeatable test environment…
… and automate all the things.
github.com/SpectoLabs/hoverfly
specto.io/blog
We run public and private training workshops: [email protected]