Streamline Integration Testcontainers IBM · tests because… •Tests do not reflect prod...
Transcript of Streamline Integration Testcontainers IBM · tests because… •Tests do not reflect prod...
![Page 1: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/1.jpg)
Streamline Integration Testing with Testcontainers—Andrew GuibertSoftware EngineerIBM
@andrew_guibert
![Page 2: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/2.jpg)
Why do we write automated tests?
EclipseCon EU 2019 | © 2019 IBM Corporation 2@andrew_guibert
![Page 3: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/3.jpg)
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
![Page 4: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/4.jpg)
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
![Page 5: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/5.jpg)
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
![Page 6: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/6.jpg)
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
![Page 7: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/7.jpg)
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
![Page 8: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/8.jpg)
The dilemma of testing
Confidence
Time given© 2019 IBM CorporationEclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 9: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/9.jpg)
The dilemma of testing
Confidence
Time given
Confidence level in tests
© 2019 IBM CorporationEclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 10: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/10.jpg)
The dilemma of testing
Confidence
Time given
Confidence level in tests
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 11: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/11.jpg)
![Page 12: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/12.jpg)
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
![Page 13: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/13.jpg)
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
![Page 14: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/14.jpg)
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
![Page 15: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/15.jpg)
“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
![Page 16: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/16.jpg)
It works on my machine.
EclipseCon EU 2019 | © 2019 IBM Corporation 16@andrew_guibert
![Page 17: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/17.jpg)
Can you spot what’s wrong here?
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 18: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/18.jpg)
Can you spot what’s wrong here?
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 19: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/19.jpg)
Testing with databases
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 20: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/20.jpg)
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
![Page 21: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/21.jpg)
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
![Page 22: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/22.jpg)
![Page 23: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/23.jpg)
What it looks like
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 24: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/24.jpg)
Using Testcontainers
EclipseCon EU 2019 | © 2019 IBM Corporation 24
• Databases
• Messaging Brokers
• External Services
• Anything in a container
@andrew_guibert
![Page 25: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/25.jpg)
Taking things a step further
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 26: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/26.jpg)
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
![Page 27: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/27.jpg)
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
![Page 28: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/28.jpg)
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
![Page 29: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/29.jpg)
Example Application
EclipseCon EU 2019 | © 2019 IBM Corporation @andrew_guibert
![Page 30: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/30.jpg)
What it looks like
EclipseCon EU 2019 | © 2019 IBM Corporation@andrew_guibert
![Page 31: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/31.jpg)
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
![Page 32: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/32.jpg)
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
![Page 33: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/33.jpg)
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
![Page 34: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/34.jpg)
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
![Page 35: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/35.jpg)
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
![Page 36: Streamline Integration Testcontainers IBM · 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](https://reader034.fdocuments.us/reader034/viewer/2022051809/60140e682fe5d57c6325fd75/html5/thumbnails/36.jpg)
36