Slide 1

Slide 1 text

TestContainers Integration testing without the hassle Sergei @bsideup Egorov

Slide 2

Slide 2 text

About me • Work at Uvita GmbH, Berlin • Apache Groovy committer • TestContainers’ co-maintainer • In love with Docker since 2013 @bsideup

Slide 3

Slide 3 text

Integration testing Why it matters?

Slide 4

Slide 4 text

@bsideup

Slide 5

Slide 5 text

@bsideup

Slide 6

Slide 6 text

Verify how your software product will behave in real- world conditions

Slide 7

Slide 7 text

Isolated from other system components to avoid false negatives

Slide 8

Slide 8 text

Real: databases, file systems, network interfaces, …

Slide 9

Slide 9 text

Grey box testing - we know some inner details, but mostly use public APIs

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Unit testing • Simulation tests are green, yay! • Everything is mocked • DB is written by others, why should I test it? @bsideup

Slide 12

Slide 12 text

Integration testing https://commons.wikimedia.org/wiki/File:Cd4007.jpg • Jeez, how did it passed the Unit testing? • Is it a smoke? @#$%! YES IT IS! • Who knew that 100w soldering gun was a bit too powerful for it? @bsideup

Slide 13

Slide 13 text

System testing https://commons.wikimedia.org/wiki/File:UART_8250_Microchip.jpg • Takes a lot of time to solder • “Oh no, the power bus is too far away from my microchip!” • Interference between the components @bsideup

Slide 14

Slide 14 text

Production @bsideup

Slide 15

Slide 15 text

Integration testing Real-world, but isolated testing Spot the issues before the real environment Can be run during the development You have to start real databases Should be cross-platform Slower than Unit testing Pros Cons @bsideup

Slide 16

Slide 16 text

Obvious solution

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Abstraction layer

Slide 19

Slide 19 text

CI friendly

Slide 20

Slide 20 text

Cross-platform

Slide 21

Slide 21 text

Docker Compose FTW! redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"
 @bsideup

Slide 22

Slide 22 text

But…

Slide 23

Slide 23 text

Declarative YAML redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"
 @bsideup

Slide 24

Slide 24 text

No ports randomization redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"
 @bsideup

Slide 25

Slide 25 text

Container per test? redis:
 image: redis
 ports:
 - "6379:6379"
 postgres:
 image: postgres
 ports:
 - "5432:5432"
 elasticsearch:
 image: elasticsearch:5.0.0
 ports:
 - "9200:9200"
 @bsideup

Slide 26

Slide 26 text

IDE integration? @bsideup

Slide 27

Slide 27 text

Fighting with Docker environment

Slide 28

Slide 28 text

There is no place like

Slide 29

Slide 29 text

There is no place like Except Docker Machine

Slide 30

Slide 30 text

Can we improve that?

Slide 31

Slide 31 text

@bsideup

Slide 32

Slide 32 text

@bsideup Remove the database

Slide 33

Slide 33 text

@bsideup Remove the database

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

TestContainers • http://github.com/testcontainers/testcontainers-java • Wraps docker-java library • Docker environment discovery (Win, Mac, Linux) • Will start docker-machine if it’s not started yet • Containers cleanup on JVM shutdown @bsideup

Slide 36

Slide 36 text

As simple as PostgreSQLContainer postgresql = new PostgreSQLContainer()
 
 GenericContainer redis = new GenericContainer("redis:3")
 .withExposedPorts(6379) @bsideup

Slide 37

Slide 37 text

Users

Slide 38

Slide 38 text

Use case #1: testing of microservices • REST service • Java, Spring Boot • Redis and PostgreSQL • Calls some other micro-services @bsideup

Slide 39

Slide 39 text

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @ContextConfiguration(initializers = Initializer.class) public abstract class AbstractIntegrationTest { @ClassRule public static PostgreSQLContainer postgreSql = new PostgreSQLContainer(); @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.6") .withExposedPorts(6379); @ClassRule public static MockServerContainer mockServer = new MockServerContainer(); } @bsideup

Slide 40

Slide 40 text

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @ContextConfiguration(initializers = Initializer.class) public abstract class AbstractIntegrationTest { @ClassRule public static PostgreSQLContainer postgreSql = new PostgreSQLContainer(); @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.6") .withExposedPorts(6379); @ClassRule public static MockServerContainer mockServer = new MockServerContainer(); } Still using all the Spring goodies! @bsideup

Slide 41

Slide 41 text

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = RANDOM_PORT) @ContextConfiguration(initializers = Initializer.class) public abstract class AbstractIntegrationTest { @ClassRule public static PostgreSQLContainer postgreSql = new PostgreSQLContainer(); @ClassRule public static GenericContainer redis = new GenericContainer("redis:3.0.6") .withExposedPorts(6379); @ClassRule public static MockServerContainer mockServer = new MockServerContainer(); } External dependencies @bsideup

Slide 42

Slide 42 text

No content

Slide 43

Slide 43 text

public class MockServerContainer extends GenericContainer { public MockServerContainer() { super("jamesdbloom/mockserver:latest"); addExposedPorts(8080); } private MockServerClient client; @Override protected void containerIsStarted(InspectContainerResponse containerInfo) { super.containerIsStarted(containerInfo); client = new MockServerClient(getContainerIpAddress(), getMappedPort(getExposedPorts().get(0))); } } @bsideup

Slide 44

Slide 44 text

public class MockServerContainer extends GenericContainer { public MockServerContainer() { super("jamesdbloom/mockserver:latest"); addExposedPorts(8080); } private MockServerClient client; @Override protected void containerIsStarted(InspectContainerResponse containerInfo) { super.containerIsStarted(containerInfo); client = new MockServerClient(getContainerIpAddress(), getMappedPort(getExposedPorts().get(0))); } } @bsideup

Slide 45

Slide 45 text

Demo

Slide 46

Slide 46 text

Use case #2: Docker as Selenium driver • Selenium/Selenide tests • No need to install Chrome/Firefox/etc • CI friendly @bsideup

Slide 47

Slide 47 text

@Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("target")); @bsideup

Slide 48

Slide 48 text

@Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("target")); RemoteWebDriver driver = chrome.getWebDriver(); driver.get("https://wikipedia.org"); WebElement searchInput = driver.findElementByName("search"); searchInput.sendKeys("Rick Astley"); searchInput.submit(); WebElement otherPage = driver.findElementByLinkText("Rickrolling"); otherPage.click(); boolean expectedTextFound = driver.findElementsByCssSelector("p") .stream() .anyMatch(element -> element.getText().contains("meme")); assertTrue("The word 'meme' is found on a page about rickrolling", expectedTextFound); @bsideup

Slide 49

Slide 49 text

@Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("target")); RemoteWebDriver driver = chrome.getWebDriver(); driver.get("https://wikipedia.org"); WebElement searchInput = driver.findElementByName("search"); searchInput.sendKeys("Rick Astley"); searchInput.submit(); WebElement otherPage = driver.findElementByLinkText("Rickrolling"); otherPage.click(); boolean expectedTextFound = driver.findElementsByCssSelector("p") .stream() .anyMatch(element -> element.getText().contains("meme")); assertTrue("The word 'meme' is found on a page about rickrolling", expectedTextFound); import org.openqa.selenium.remote.RemoteWebDriver; @bsideup

Slide 50

Slide 50 text

@Rule public BrowserWebDriverContainer chrome = new BrowserWebDriverContainer() .withDesiredCapabilities(DesiredCapabilities.chrome()) .withRecordingMode(RECORD_ALL, new File("target")); RemoteWebDriver driver = chrome.getWebDriver(); driver.get("https://wikipedia.org"); WebElement searchInput = driver.findElementByName("search"); searchInput.sendKeys("Rick Astley"); searchInput.submit(); WebElement otherPage = driver.findElementByLinkText("Rickrolling"); otherPage.click(); boolean expectedTextFound = driver.findElementsByCssSelector("p") .stream() .anyMatch(element -> element.getText().contains("meme")); assertTrue("The word 'meme' is found on a page about rickrolling", expectedTextFound); @bsideup

Slide 51

Slide 51 text

Demo

Slide 52

Slide 52 text

• http://testcontainers.org • testcontainers/testcontainers-java-examples • Integration testing is a must • Docker works great for testing • Unified env for Dev and CI @bsideup

Slide 53

Slide 53 text

Questions?

Slide 54

Slide 54 text

@bsideup bsideup

Slide 55

Slide 55 text

https://commons.wikimedia.org/wiki/File:Thats_all_folks.svg