Microservice Testing Techniques:
Mocks vs Service Virtualization vs Remocal Tools
Daniel Bryant
@danielbryantuk
@danielbryantuk
tl;dr
2
• The inner dev/test loop can be painful with microservices & Kubernetes
• The pain increases as the number of services within your system increase
• You have options with +/- : mocks, service virtualisation, remocal tools
• Telepresence enables remote-to-local “remocal” development
• Choose your dev/test approach wisely!
@danielbryantuk
Testing Microservices: What to use, when
3
Mocks Service virtualization Remocal
Unit tests ✅
Integration tests ✅ ✅
Component tests ✅ ✅ ✅
End-to-end tests ✅ ✅
Poking around an
existing API
✅
Building an API ✅ ✅
@danielbryantuk
4
Setting the Scene
5
@danielbryantuk
Inner and outer dev loops
6
Hat tip to Mitch Denny (now 404): https://mitchdenny.com/the-inner-loop/
http://jorgemoral.es/posts/2020_03_17-develop_apps…
Local:
“You’ll pry localhost from
my cold dead hands!"
Remote:
“CI/CD/envs as a Service”
@danielbryantuk
You are building containerised microservices, right?
7
@danielbryantuk
SOA: There’s just more stu
ff
(and dependencies)
8
@danielbryantuk
So, let’s run everything locally…
9
@danielbryantuk
tl;dr
10
“My laptop only has 16GB
RAM and you’re asking it
to run Kubernetes and a
JVM and a database???”
@danielbryantuk
Okay, let’s run everything remotely…
11
@danielbryantuk
Inner and outer dev loops with K8s
can be the same
12
• Write code
• Build container
• Push to registry
• Deploy to cluster
• Test
And can be …. SLOW.
@danielbryantuk
Automation can help…
13
@danielbryantuk
tl;dr
14
But you want a really fast inner loop (no docker build/push) …
@danielbryantuk
tl;dr
15 And you want to use your own tools.
@danielbryantuk
tl;dr
16
And you want to avoid the “works on my machine”
production vs local dev debacle
Options and Tradeo
ff
s
17
@danielbryantuk
Testing Strategies in a Microservice Architecture
18
https://martinfowler.com/articles/microservice-testing/
@danielbryantuk
What are your options?
19
• Mocks
• Mockito, PowerMock(ito), EasyMock, JMockit
• Service virtualisation
• TestContainers, LocalStack, (Embedded Datastore/middleware?)
• Hoverfly, WireMock, Mountebank
• Remocal tooling
• Devspace, Okteto
• Telepresence, Gefyra
@danielbryantuk
Mocks
20
https://site.mockito.org/
@danielbryantuk
Mocks
21
• Good
• Idiomatic (familiar language constructs etc)
• Quick and cheap
• Can help build your mental model (TDD-like)
• Bad
• Implicit assumptions coded in (“the three realities conundrum”)
• Di
ff
icult to keep up to date and share/coordinate
• Needs a test runner
@danielbryantuk
Testing Microservices: What to use, when
22
Mocks
Unit tests ✅
Integration tests ✅
Component tests ✅
End-to-end tests
Poking around an existing API
Building an API ✅
@danielbryantuk
Service virtualisation
23
https://livebook.manning.com/book/testing-java-microservices/chapter-9/47
@danielbryantuk
Service virtualisation
24
https://livebook.manning.com/book/testing-java-microservices/chapter-9/47
@danielbryantuk
TestContainers
25
@danielbryantuk
Service virtualisation
26
• Good
• More realistic than mocks (can be the “real thing”, albeit resource constrained)
• Enables (wire-level) deterministic test of failure scenarios
• Easy to use large(r) amounts of data
• Bad
• Slower than mocks to initialise
• Can be tricky to configure
• Challenging to keep up to date and share (unless you use dedicated tooling/SaaS)
@danielbryantuk
Testing Microservices: What to use, when
27
Service virtualization
Unit tests
Integration tests ✅
Component tests ✅
End-to-end tests ✅
Poking around an existing API
Building an API ✅
@danielbryantuk
Remote-to-local “Remocal” tooling
28
@danielbryantuk
Remocal tooling
29
• Good
• Enables testing against (and poking of) the real thing
• Data sets can be large scale (as resources can be scaled on remote cluster)
• No need to build mental model of dependency (both good and bad!)
• Bad
• Need a solid network connection
• Mutating remote shared state can be an issue
• Lack of language bindings
@danielbryantuk
Testing Microservices: What to use, when
30
Remocal
Unit tests
Integration tests
Component tests ✅
End-to-end tests ✅
Poking around an existing API ✅
Building an API
@danielbryantuk
31
@danielbryantuk
What is Telepresence
32
• “Fancy Kubernetes VPN for development”
• “kubectl port-forward on steroids”
• “kubectl proxy all-the-things”
• A network bridge between your laptop and the Kubernetes cluster
@danielbryantuk
Telepresence: Remote-to-local bridge
33
@danielbryantuk
Telepresence: Local-to-Remote bridge
34
www.youtube.com/watch?v=W_a3aErN3NU
Demo Time!
35
@danielbryantuk
Telepresence: Local-to-Remote bridge
36
telepresence connect
• Open a tunnel to the remote cluster; exposes “in-cluster” services/network/DNS
telepresence intercept my-service —port 3000
• Re-routes (intercepts) tra
ff
ic to my-service in the remote cluster to my local machine
telepresence login & telepresence intercept
• Create preview URL to isolate and share results of the intercept
@danielbryantuk
Telepresence: Local-to-Remote bridge
$ telepresence intercept dataprocessingnodeservice --port 3000
37
Using deployment dataprocessingnodeservice
intercepted
State : ACTIVE
Destination : 127.0.0.1:3000
Intercepting: HTTP requests that match all of:
header("x-telepresence-intercept-id") ~= regexp ("76a1e848-1829-74x-1138-e3294c1e9119:dataprocessingnodeservice")
Preview URL : https://[random-subdomain].preview.edgestack.me
www.youtube.com/watch?v=W_a3aErN3NU
@danielbryantuk
Headers for selective intercepts
38
Docker Desktop Extension for Telepresence
39
getambassador.io/testing-with-telepresence-and-docker
40
Wrapping up
41
@danielbryantuk
Testing Microservices: What to use, when
42
Mocks Service virtualization Remocal
Unit tests ✅
Integration tests ✅ ✅
Component tests ✅ ✅ ✅
End-to-end tests ✅ ✅
Poking around an
existing API
✅
Building an API ✅ ✅
@danielbryantuk
Conclusion
43
• The inner dev/test loop can be painful with microservices & Kubernetes
• The pain increases as the number of services within your system increase
• You have options with +/- : mocks, service virtualisation, remocal tools
• Telepresence enables remote-to-local “remocal” development
• Choose your dev/test approach wisely!
@danielbryantuk
Thank you! Questions?
44
getambassador.io/products/telepresence
getambassador.io/testing-with-telepresence-and-docker
Easy Debugging of Spring Boot Microservices Running on
Kubernetes
AL Developer O
ff
ice Hours
db@datawire.io / @danielbryantuk

CraftConf 2023 "Microservice Testing Techniques: Mocks vs Service Virtualization vs Remocal Tools