Slide 1

Slide 1 text

2018-09-07
 Berlin / BED-Con Self-Contained integration tests with Docker and Testcontainers Michael Vitz https://pixabay.com/en/construction-site-crane-pier-1156575 @michaelvitz #bedcon

Slide 2

Slide 2 text

Michael Vitz
 Senior Consultant at INNOQ • Build, run, and maintain JVM applications • JavaSPEKTRUM column owner • ❤ Clojure

Slide 3

Slide 3 text

www.innoq.com OFFICES Monheim Berlin Offenbach Munich Zurich FACTS ~125 employees Privately owned Vendor-independent SERVICES Strategy & technology consulting Digital business models Software architecture & development Digital platforms & infrastructures Knowledge transfer, coaching & trainings CLIENTS Finance Telecommunications Logistics E-commerce Fortune 500 SMBs Startups

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

UI Integrated UI Service Service Integration Unit Implementation Unit

Slide 7

Slide 7 text

UI Integrated UI Service Service Integration Unit Implementation Unit https://martinfowler.com/bliki/TestPyramid.html https://www.thoughtworks.com/de/insights/blog/introducing-software-testing-cupcake-anti-pattern https://labs.spotify.com/2018/01/11/testing-of-microservices Testing Pyramid Honeycomb Ice Cream Cone

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

https://8thlight.com/blog/uncle-bob/2012/08/13/the-clean-architecture.html

Slide 10

Slide 10 text

http://www.dossier-andreas.net/software_architecture/ports_and_adapters.html Ports-And-Adapters

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

Testcontainers • Launch Docker containers from Java • Test data access layer • UI browser tests • Requires Docker • Requires Java 8 https://www.testcontainers.org

Slide 13

Slide 13 text

@ClassRule public static GenericContainer postgres = new GenericContainer("postgres:latest") .withEnv("POSTGRES_PASSWORD", "mysecretpassword"); private Connection create() throws Exception { String host = postgres.getContainerIpAddress(); int port = postgres.getMappedPort(5432); String url = "jdbc:postgresql://"+host+":"+port+"/postgres"; String user = "postgres"; String password = "mysecretpassword"; return DriverManager.getConnection(url, user, password); } Generic Containers

Slide 14

Slide 14 text

static String WAIT_PATTERN = ".*database system is ready to accept connections.*\\s"; @ClassRule public static GenericContainer postgres = new GenericContainer("postgres:latest") .withEnv("POSTGRES_PASSWORD", "mysecretpassword") .waitingFor(forLogMessage(WAIT_PATTERN, 2)); Container != Application

Slide 15

Slide 15 text

@ClassRule public static PostgreSQLContainer postgres = new PostgreSQLContainer() .withPassword("mysecretpassword"); private Connection create() throws Exception { String url = postgres.getJdbcUrl(); String user = postgres.getUsername(); String password = postgres.getPassword(); return DriverManager.getConnection(url, user, password); } Database Container

Slide 16

Slide 16 text

@Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("build")); @Test public void test() { WebDriver driver = chrome.getWebDriver(); } Selenium Container

Slide 17

Slide 17 text

Testcontainers 2.0 • Cleanup API • Entangle from JUnit 4 rules https://github.com/rnorth/containercore/pull/1

Slide 18

Slide 18 text

www.innoq.com innoQ Deutschland GmbH Krischerstr. 100 40789 Monheim am Rhein Germany +49 2173 3366-0 Ohlauer Str. 43 10999 Berlin Germany +49 2173 3366-0 Ludwigstr. 180E 63067 Offenbach Germany +49 2173 3366-0 Kreuzstr. 16 80331 München Germany +49 2173 3366-0 innoQ Schweiz GmbH Gewerbestr. 11 CH-6330 Cham Switzerland +41 41 743 0116 Thanks! Questions? Michael Vitz [email protected] michaelvitz +49 151 19116015 https://speakerdeck.com/michaelvitz/testcontainers https://github.com/mvitz/javaspektrum-testcontainers