Streamline Integration Testcontainers IBM · tests because… •Tests do not reflect prod...

36
Streamline Integration Testing with Testcontainers Andrew Guibert Software Engineer IBM @andrew_guibert

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

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

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

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

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

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

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

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

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

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

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
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

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

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

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

“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

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

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

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

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

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

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
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

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

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

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

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

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

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

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

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

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

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

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

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

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

36