The needs for test automation changes in a microservice architecture compared to a monolith or traditional SOA. Even more so if we combine microservices with continuous delivery. What and how we choose to test has a large impact on how successfull we will be with microservices. If we choose wisely we can take advantage of the architecture to get good test coverage and confidence relative to the effort. This talk gives guidance about effective levels to test at and how to use DDD techniques to guide the effort.
2. ABOUT ME
Christian Horsdal Gammelgaard
Independent Consultant
www.horsdal-consult.dk
c.horsdal@gmail.com
@chr_horsdal
@horsdal@mastodon.nu
3. WHAT TO TEST?
Confidence to deploy to production
Maintenance effort
100+ services
Bringing it all up locally is not an option
4. THE 6 CHARACTERISTICS
OF MICROSERVICES
A microservice
1. Is responsible for a single capability.
2. Is individually deployable.
3. Consists of one or more processes.
4. Owns its own data store.
5. A small team can maintain a handful of
microservices.
6. Is replaceable.
6. WHAT IS THE SCOPE OF A MICROSERVICE?
The primary driver for
scoping microservices is
business capabilities
A microservice is responsible
for a single business
capability
12. THE TEST PYRAMID
• System level test:
• Test the complete system
• Black box
• Most important use cases
13. THE TEST PYRAMID
• Service level test:
• Test one complete
microservice
• Other microservices are
stubbed
• Grey box
• All important scenarios
• Positive and negative
• Test the business capability
• Contract tests
14. THE TEST PYRAMID
• Unit test:
• Test a unit of behavior
• White box
• Detailed
• Try to avoid mocks
18. SERVICE LEVEL TESTS
Loyalty Program microservice
Mocked Notifications
microservice
Send command
Send canned response
19. SERVICE LEVEL TESTS
Service level test
scenario
Mocked
notifications
microservice
Loyalty Program
microservice
Send canned event
through real queue
Real HTTP request
Integration test process
20. SERVICE LEVEL TESTS
Mocking other services:
The test process is a .NET app
MVC can run in any .NET app
RabbitMQ subscriptions can run in any .NET app
⇒ Just create endpoints with MVC
⇒ And just use the bus
21. MOCKING OTHER SERVICES
public class NotificationsMock : Controller
{
public static bool ReceivedNotification = false;
[HttpPost("/notify")]
public OkResult Notify()
{
ReceivedNotification = true;
return Ok();
}
}
22. HOSTING MOCKED SERVICES IN TEST PROCESS
public MocksHost(int port)
{
this.hostForMocks =
Host
.CreateDefaultBuilder()
.ConfigureWebHostDefaults(x => x
.Configure(app =>
app
.UseRouting()
.UseEndpoints(opt => opt.MapControllers()))
.UseUrls($"http://localhost:{port}"))
.Build();
new Thread(() => this.hostForMocks.Run()).Start();
}
23. public class RegisterUserAndGetNotification : IDisposable
{
...
public RegisterUserAndGetNotification()
{
this.serviceMock = new MocksHost(mocksPort);
this.loyaltyProgramHost = new HostBuilder()
.ConfigureWebHost(x => x
.UseStartup<Startup>()
.UseTestServer())
.Start();
this.sut = this.loyaltyProgramHost.GetTestClient();
}
[Fact]
public async Task Scenario()
{
await RegisterNewUser();
await PublishSpecialOffer();
AssertNotificationWasSent();
}
27. SERVICE LEVEL TESTS
Loyalty Program microservice
Mocked Notifications
microservice
Send command
Send canned response
28. SERVICE LEVEL TESTS
Service level test
scenario
Mocked
notifications
microservice
Loyalty Program
microservice
Send canned event
through real queue
Real HTTP request
Integration test process
29. TEST THE CONTRACT
Contract test based on
Mocked Notifications
microservice
Send command
Send real response
Notications
microservice
Assert
real response= canned response
30. MAKE THE IMPLICIT EXPLICIT
Service level test
scenario
Mocked
notifications
microservice
Loyalty Program
microservice
Send canned event
through real queue
Real HTTP request
Integration test process
37. TL;DR: FOCUS ON SERVICE LEVEL TESTS
• Service level test:
• Test one complete
microservice
• Other microservices are
stubbed
• Grey box
• All important scenarios
• Positive and negative
• Test the business capability
• Contract tests
38. ABOUT ME
Christian Horsdal Gammelgaard
Independent Consultant
www.horsdal-consult.dk
c.horsdal@gmail.com
@chr_horsdal