Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.
TestContainers
Integration testing without the hassle
Anton Arhipov
@antonarhipov
Why integration testing?
1. # of tests per time unit
1. # of tests per time unit
2. Complex setup
1. Reproducible environment
1. Reproducible environment
2. Both for development and CI
1. Reproducible environment
2. Both for development and CI
3. Isolated
1. Reproducible environment
2. Both for development and CI
3. Isolated
4. As real as possible
1. Reproducible environment
2. Both for development and CI
3. Isolated
4. As real as possible
5. Cross-platform
1. Reproducible environment
2. Both for development and CI
3. Isolated
4. As real as possible
5. Cross-platform
6. Easy to...
TestContainers to the rescue!
How does it work?
https://github.com/docker-java/docker-java
Docker environment discovery
Will start docker machine if it’...
GenericContainer redis =
new GenericContainer("redis:3.0.6")
.withExposedPorts(6379);
GenericContainer redis =
new GenericContainer("redis:3.0.6")
.withExposedPorts(6379);
PostgreSQLContainer postgreSQLContai...
https://github.com/testcontainers/testcontainers-java
Microservices!11
REST service
Java, Spring Boot
Redis & PostgreSQL
Calls to some other application
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class,
webEnvironment = WebEnvironment.R...
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class,
webEnvironment = WebEnvironment.R...
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = DemoApplication.class,
webEnvironment = WebEnvironment.R...
public class MockServerContainer<SELF extends MockServerContainer<SELF>>
extends GenericContainer<SELF> {
public MockServe...
public class MockServerContainer<SELF extends MockServerContainer<SELF>>
extends GenericContainer<SELF> {
public MockServe...
MockServerClient client = …
client.when(
HttpRequest.request("/analytics/issues/" +
project.projectId + "/app").withMethod...
https://github.com/bsideup/testcontainers-microservices-example
How to test Java agents?
https://github.com/bsideup/javaagent-boilerplate
https://www.testcontainers.org
https://twitter.com/testcontainers
https://github.com/testcontainers
https://testcontainers...
anton@zeroturnaround.com
@antonarhipov
slideshare.net/arhan
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
JavaDay Kiev 2017 - Integration testing with TestContainers
Próxima SlideShare
Cargando en…5
×

JavaDay Kiev 2017 - Integration testing with TestContainers

Integration testing for Java developers.

  • Inicia sesión para ver los comentarios

JavaDay Kiev 2017 - Integration testing with TestContainers

  1. 1. TestContainers Integration testing without the hassle
  2. 2. Anton Arhipov @antonarhipov
  3. 3. Why integration testing?
  4. 4. 1. # of tests per time unit
  5. 5. 1. # of tests per time unit 2. Complex setup
  6. 6. 1. Reproducible environment
  7. 7. 1. Reproducible environment 2. Both for development and CI
  8. 8. 1. Reproducible environment 2. Both for development and CI 3. Isolated
  9. 9. 1. Reproducible environment 2. Both for development and CI 3. Isolated 4. As real as possible
  10. 10. 1. Reproducible environment 2. Both for development and CI 3. Isolated 4. As real as possible 5. Cross-platform
  11. 11. 1. Reproducible environment 2. Both for development and CI 3. Isolated 4. As real as possible 5. Cross-platform 6. Easy to set up, use & maintain
  12. 12. TestContainers to the rescue!
  13. 13. How does it work? https://github.com/docker-java/docker-java Docker environment discovery Will start docker machine if it’s not started yet Containers cleanup on JVM shutdown
  14. 14. GenericContainer redis = new GenericContainer("redis:3.0.6") .withExposedPorts(6379);
  15. 15. GenericContainer redis = new GenericContainer("redis:3.0.6") .withExposedPorts(6379); PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:9.6.2") .withUsername(POSTGRES_USERNAME) .withPassword(POSTGRES_PASSWORD);
  16. 16. https://github.com/testcontainers/testcontainers-java
  17. 17. Microservices!11 REST service Java, Spring Boot Redis & PostgreSQL Calls to some other application
  18. 18. @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = AbstractIntegrationTest.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();
  19. 19. @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = AbstractIntegrationTest.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!
  20. 20. @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = DemoApplication.class, webEnvironment = WebEnvironment.RANDOM_PORT) @ContextConfiguration(initializers = AbstractIntegrationTest.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
  21. 21. public class MockServerContainer<SELF extends MockServerContainer<SELF>> extends GenericContainer<SELF> { 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))); } }
  22. 22. public class MockServerContainer<SELF extends MockServerContainer<SELF>> extends GenericContainer<SELF> { 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))); } }
  23. 23. MockServerClient client = … client.when( HttpRequest.request("/analytics/issues/" + project.projectId + "/app").withMethod("GET"), Times.unlimited()) .respond(singleIssueResponse());
  24. 24. https://github.com/bsideup/testcontainers-microservices-example
  25. 25. How to test Java agents?
  26. 26. https://github.com/bsideup/javaagent-boilerplate
  27. 27. https://www.testcontainers.org https://twitter.com/testcontainers https://github.com/testcontainers https://testcontainers.slack.com
  28. 28. anton@zeroturnaround.com @antonarhipov slideshare.net/arhan

×