One thing often overlooked in API driven architectures, or even microservice based architectures, is how to reliably simulate APIs that our consumers depend on. If this isn’t done well, inter-service bugs are often not discovered until the end-to-end testing tier, meaning our feedback cycle is extremely slow.
Some of the things we may have trouble with are dealing with APIs which change on a regular basis, APIs which are non-deterministic, injecting faults into APIs, tedious to set up stubs and more.
In this talk, I’ll be introducing testing problems that we face when dealing with external APIs. Then I’ll be solving them with some practical, take-home examples, leveraging open source API simulation tools in the process. I’ll also be highlighting various anti-patterns that I’ve noticed people adopting over the years, and explain why they should be avoided.
9. @mogronalol 4
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
10. @mogronalol 4
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
11. @mogronalol 4
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
12. @mogronalol 4
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
13. @mogronalol 5
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
30. @mogronalol 10
Airline Booking Application
• Microservice architecture
• Integration with third party services
• Integration with legacy internal services
31. @mogronalol 10
Airline Booking Application
• Microservice architecture
• Integration with third party services
• Integration with legacy internal services
• Heavy resource consumption
32. @mogronalol 10
Airline Booking Application
• Microservice architecture
• Integration with third party services
• Integration with legacy internal services
• Heavy resource consumption
• A standard CI/CD pipeline
74. @mogronalol 19
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
105. @mogronalol 28
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
113. @mogronalol 30
JUnit Example
@Test
public 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));
}
114. @mogronalol 30
JUnit Example
@Rule
public HoverflyRule hoverflyRule = HoverflyRule.inCaptureMode(“src/test/
resources/external.json”);
@Test
public 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));
}
116. @mogronalol 32
JUnit Example
@Test
public 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.inSimulationMode(classpath(“external.json”));
190. @mogronalol
Request
GET www.foo.com /name/tim ?token=bar
Matchers Score Match?
MISS www.foo.com miss 1
MISS www.foo.com /name/tim 2
GET www.foo.com /name/tim miss 3
45
Debugging
Closest?
191. @mogronalol
Request
GET www.foo.com /name/tim ?token=bar
Matchers Score Match?
MISS www.foo.com miss 1
MISS www.foo.com /name/tim 2
GET www.foo.com /name/tim miss 3
45
Debugging
Closest?
230. @mogronalol 53
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
250. @mogronalol 58
First Class Simulations
Run and
Capture
Service Tests
Transform to
simulation
Publish
Virtual
Service
251. @mogronalol 58
First Class Simulations
Run and
Capture
Service Tests
Parse
Contract
Transform to
simulation
Publish
Virtual
Service
252. @mogronalol 58
First Class Simulations
Run and
Capture
Service Tests
Parse
Contract
Transform to
simulation
Parse
Documentati
on
Publish
Virtual
Service
254. @mogronalol 59
Ownership
Own Consumer Own Producer Strategy
Integration
Contract Testing
Consumer Driven
Contract Testing
Virtual Service as
First Class Citizen
273. @mogronalol 64
Agenda
• The problems with testing API driven architectures
• Introduction to API simulation
• Applying API simulation patterns
• Data management
• Conclusion
277. @mogronalol 65
Conclusion
• Reduced cost be replacing real resources
• Improved deployment time with API simulations
• Less dependance on end-to-end testing
278. @mogronalol 65
Conclusion
• Reduced cost be replacing real resources
• Improved deployment time with API simulations
• Less dependance on end-to-end testing
• Advanced patterns for simplifying API simulations
279. @mogronalol 65
Conclusion
• Reduced cost be replacing real resources
• Improved deployment time with API simulations
• Less dependance on end-to-end testing
• Advanced patterns for simplifying API simulations
• Improved feedback cycle