Streamline Integration Testcontainers IBM · tests because… •Tests do not reflect prod...
Transcript of Streamline Integration Testcontainers IBM · tests because… •Tests do not reflect prod...
Streamline Integration Testing with Testcontainers—Andrew GuibertSoftware EngineerIBM
@andrew_guibert
Why do we write automated tests?
EclipseCon EU 2019 | © 2019 IBM Corporation 2@andrew_guibert
Why do we write automated tests?
To have confidence that our application works the way we want it to.
EclipseCon EU 2019 | © 2019 IBM Corporation 3@andrew_guibert
What type of testing gives us the most confidence?
EclipseCon EU 2019 | © 2019 IBM Corporation 4
Source: If applicable, describe source origin
System/UITesting
Unit Testing
Integration Testing
@andrew_guibert
EclipseCon EU 2019 | © 2019 IBM Corporation 5
Which type of tests contribute more to your confidence in your automated tests?
Unit/Mock tests24%
Integration (i.e. "live server") tests76%
@andrew_guibert
4/4 Unit Tests Passing
Faucet turns on
Faucet turns off
Drain works
Sink does notoverflow
© 2019 IBM CorporationEclipseCon EU 2019 | © 2019 IBM Corporation
EclipseCon EU 2019 | © 2019 IBM Corporation
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
What we have time to do
EclipseCon EU 2019 | © 2019 IBM Corporation 7
Source: If applicable, describe source origin
Unit Testing
Integration Testing
Manual testing if I remember and am not swamped
@andrew_guibert
The dilemma of testing
Confidence
Time given© 2019 IBM CorporationEclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
The dilemma of testing
Confidence
Time given
Confidence level in tests
© 2019 IBM CorporationEclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
The dilemma of testing
Confidence
Time given
Confidence level in tests
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
The Twelve-Factor App
EclipseCon EU 2019 | © 2019 IBM Corporation 12
VII. Port Binding
VIII.Concurrency
IX. Disposability
X. Dev-Prod Parity
XI. Logs
XII. Admin Processes
I. Codebase
II. Dependencies
III. Config
IV. Backing Services
V. Build, Release, Run
VI. Stateless Processes
@andrew_guibert
The Twelve-Factor App
EclipseCon EU 2019 | © 2019 IBM Corporation 13
VII. Port Binding
VIII.Concurrency
IX. Disposability
X. Dev-Prod Parity
XI. Logs
XII. Admin Processes
I. Codebase
II. Dependencies
III. Config
IV. Backing Services
V. Build, Release, Run
VI. Stateless Processes
@andrew_guibert
Dev-Prod Parity
EclipseCon EU 2019 | © 2019 IBM Corporation 14
Keep development, staging, and production as similar as possible
Comprised of three common issues:– The time gap– The personnel gap– The tools gap
@andrew_guibert
“The twelve-factor developer resists the urge to use different backing services between development and production, even when adapters theoretically abstract away any differences in backing services.”
EclipseCon EU 2019 | © 2019 IBM Corporation 15
Dev-Prod Parity
@andrew_guibert
It works on my machine.
EclipseCon EU 2019 | © 2019 IBM Corporation 16@andrew_guibert
Can you spot what’s wrong here?
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
Can you spot what’s wrong here?
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
Testing with databases
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
EclipseCon EU 2019 | © 2019 IBM Corporation 20
Why do we do this?
• Easy to set up
• Faster than the real thing
• Been doing it this way for >10 years
@andrew_guibert
EclipseCon EU 2019 | © 2019 IBM Corporation 21
Containers
• Combines everything needed to run a service into a single unit
• Standard way to manage
• Lightweight and disposable
• Typically configured via env
@andrew_guibert
What it looks like
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
Using Testcontainers
EclipseCon EU 2019 | © 2019 IBM Corporation 24
• Databases
• Messaging Brokers
• External Services
• Anything in a container
@andrew_guibert
Taking things a step further
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
Motivation
Devs lack confidence in their tests because…
• Tests do not reflect prod
• Writing tests takes too long
• Setup is too complex
11
11
10
7
18
9
0 10 20
WRITING TESTS IS TOO COMPLICATED OR TIME …
OUR APP IS TOO BIG/COMPLEX TO TEST
RUNNING THE TESTS WE HAVE TAKES TOO LONG
DEBUGGING A TEST THAT FAILS TAKES TOO LONG
THE TESTS WE HAVE DO NOT ACCURATELY REPRESENT HOW …
SETTING UP OR CONFIGURING TESTS IS TOO COMPLICATED
What factors are limiting the confidence in your automated tests?
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
1) Easy to set up 2) Works with• Java EE• Jakarta EE• MicroProfile
3) Provide true-to-production tests
MicroShed Testing
EclipseCon EU 2019 | © 2019 IBM Corporation 27@andrew_guibert
MicroShed Testing
Test client JVM Docker network *
App container
App containerApp containerOther container
resources(e.g. DB)
User test code
MicroShed Testing
Testcontainers
docker-java
Convenience libs
(e.g. REST client)controls
* May be on different machine from test client
Test request/responses
@andrew_guibert
Example Application
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
What it looks like
EclipseCon EU 2019 | © 2019 IBM Corporation@andrew_guibert
Automatically discover, build, and start services
EclipseCon EU 2019 | © 2019 IBM Corporation 31
a) Provide container label
b) Locate Dockerfile
c) Use default image
@andrew_guibert
Wait for app container to be ready
EclipseCon EU 2019 | © 2019 IBM Corporation 32
• Defaults to app context root
• Can use MP Health 2.0 readiness check
• Can supply path manually
@andrew_guibert
Inject REST client
EclipseCon EU 2019 | © 2019 IBM Corporation 33
• Can re-use class from app
• Can define own client
• Automatically configured for app container
@andrew_guibert
Drive requests on running app container
EclipseCon EU 2019 | © 2019 IBM Corporation 34
• Real HTTP requests
• Parameters and return values converted with JSON-B
• JWT tokens applied automatically
@andrew_guibert
Where to learn more
EclipseCon EU 2019 | © 2019 IBM Corporation 35
Testcontainers– testcontainers.org– github.com/testcontainers/testcontai
ners-java
MicroShed–microshed.org/microshed-testing/– github.com/MicroShed/microshed-
testing
Demo Used– github.com/aguibert/oc1-demo
Andrew Guibert@andrew_guibert@aguibert
@andrew_guibert
36