Slide 1

Slide 1 text

1 Better Testing With Testcontainers

Slide 2

Slide 2 text

2 Gerrit Meier ● Software Engineer at Neo4j ● Team Spring Data Neo4j and Neo4j OGM ●

Slide 3

Slide 3 text

Software Testing

Slide 4

Slide 4 text

Software Testing Integration testing

Slide 5

Slide 5 text

Integration testing “Testing our application interacting with an external system.” -me

Slide 6

Slide 6 text

Common setups 6

Slide 7

Slide 7 text

Common setups 7 Embedded

Slide 8

Slide 8 text

Common setups 8 Embedded Local instance

Slide 9

Slide 9 text

Common setups 9 Embedded Local instance Remote instance

Slide 10

Slide 10 text

Common setups 10 Embedded Local instance Remote instance Docker

Slide 11

Slide 11 text

11 Embedded

Slide 12

Slide 12 text

12 Pros - Clean instance - Portable test suite - No side-effects from the outside Embedded

Slide 13

Slide 13 text

13 Cons Pros - Dependency version - JVM version - Non-existing network - Memory allocation - Post-mortem debugging - Clean instance - Portable test suite - No side-effects from the outside Embedded

Slide 14

Slide 14 text

14 Local instance

Slide 15

Slide 15 text

15 Pros - Separation of application and database - Network - No version conflicts - Post-mortem debugging Local instance

Slide 16

Slide 16 text

16 Cons Pros - Custom configuration - Setup needed for colleagues and CI - Lifecycle control on CI - Separation of application and database - Network - No version conflicts - Post-mortem debugging Local instance

Slide 17

Slide 17 text

17 Remote instance a.k.a. the “Test instance”

Slide 18

Slide 18 text

18 Pros - One time setup - Always on - No lifecycle management needed Remote instance a.k.a. the “Test instance”

Slide 19

Slide 19 text

19 Cons Pros - Shared database state - Conflicting tests - Unexpected test failures - Post-mortem debugging - One time setup - Always on - No lifecycle management needed Remote instance a.k.a. the “Test instance”

Slide 20

Slide 20 text

20 Docker

Slide 21

Slide 21 text

21 Pros - Orchestration within test setup - Isolated testing Docker

Slide 22

Slide 22 text

22 Cons Pros - Java API “command line”-style - Ready(?) - Orchestration within test setup - Isolated testing Docker

Slide 23

Slide 23 text

Testcontainers - Fluent wrapper around Docker API - Support for a lot of systems out-of-the-box - Aware of readiness(!) - Seamless integration with JUnit (and Spock) - Networks of multiple containers - Copy data for testing

Slide 24

Slide 24 text

24 Spring Boot Spring Data Neo4j JUnit 5

Slide 25

Slide 25 text

25 MaterialIT.java @SpringBootTest class MaterialIT { private final MaterialRepository repository; private final Neo4jClient neo4jClient; @Autowired MaterialIT(MaterialRepository repository, Neo4jClient neo4jClient) { this.repository = repository; this.neo4jClient = neo4jClient; } }

Slide 26

Slide 26 text

26 pom.xml Add Testcontainers org.testcontainers junit-jupiter 1.17.6 test

Slide 27

Slide 27 text

27 MaterialIT.java @SpringBootTest class MaterialIT { static GenericContainer> container = new GenericContainer<>("neo4j:5") .withExposedPorts(7687) .withEnv("NEO4J_AUTH", "neo4j/verysecret"); }

Slide 28

Slide 28 text

28 MaterialIT.java @SpringBootTest class MaterialIT { static GenericContainer> container = new GenericContainer<>("neo4j:5") .withExposedPorts(7687) .withEnv("NEO4J_AUTH", "neo4j/verysecret"); @BeforeAll static void startDatabase() { container.start(); } }

Slide 29

Slide 29 text

29 MaterialIT #testingProperties @DynamicPropertySource static void testingProperties( DynamicPropertyRegistry registry) { registry.add("spring.neo4j.uri", () -> "neo4j://" + container.getHost() + ":" + container.getMappedPort(7687)); registry.add("spring.neo4j.authentication.username", () -> "neo4j"); registry.add("spring.neo4j.authentication.password", () -> "verysecret"); }

Slide 30

Slide 30 text

30 MaterialIT.java @SpringBootTest @Testcontainers class MaterialIT { @Container static GenericContainer> container = new GenericContainer<>("neo4j:5") .withExposedPorts(7687) .withEnv("NEO4J_AUTH", "neo4j/verysecret"); }

Slide 31

Slide 31 text

Testcontainers Neo4j - Neo4j readiness aware - Simplified env parameters for configuration - Exposes Bolt and HTTP port as default - Neo4j specific API - Admin user setup - Connection related information - Neo4j Labs Plugin activation

Slide 32

Slide 32 text

32 pom.xml Add Testcontainers org.testcontainers neo4j 1.17.6 test

Slide 33

Slide 33 text

33 MaterialIT.java @SpringBootTest @Testcontainers class MaterialIT { @Container static Neo4jContainer> container = new Neo4jContainer<>("neo4j:5") .withAdminPassword("verysecret") }

Slide 34

Slide 34 text

34 MaterialIT #testingProperties @DynamicPropertySource static void testingProperties( DynamicPropertyRegistry registry) { registry.add("spring.neo4j.uri", container::getBoltUrl); registry.add("spring.neo4j.authentication.username", () -> "neo4j"); registry.add("spring.neo4j.authentication.password", container::getAdminPassword); }

Slide 35

Slide 35 text

35 MaterialIT #pluginCall void pluginCall() { String apocVersion = neo4jClient .query("RETURN apoc.version()") .fetchAs(String.class) .one().get(); Assertions.assertThat(apocVersion) .isEqualTo("5.1.0"); }

Slide 36

Slide 36 text

36 MaterialIT #pluginCall void pluginCall() { String apocVersion = neo4jClient .query("RETURN apoc.version()") .fetchAs(String.class) .one().get(); Assertions.assertThat(apocVersion) .isEqualTo("5.1.0"); } Unknown function 'apoc.version'

Slide 37

Slide 37 text

37 MaterialIT.java @SpringBootTest @Testcontainers class MaterialIT { @Container static Neo4jContainer> container = new Neo4jContainer<>("neo4j:5") .withAdminPassword("verysecret") .withLabsPlugins(Neo4jLabsPlugin.APOC) }

Slide 38

Slide 38 text

38 MaterialIT #pluginCall void pluginCall() { String apocVersion = neo4jClient .query("RETURN apoc.version()") .fetchAs(String.class) .one().get(); Assertions.assertThat(apocVersion) .isEqualTo("5.1.0"); }

Slide 39

Slide 39 text

39 MaterialIT #pluginCall void pluginCall() { String apocVersion = neo4jClient .query("RETURN apoc.version()") .fetchAs(String.class) .one().get(); Assertions.assertThat(apocVersion) .isEqualTo("5.1.0"); }

Slide 40

Slide 40 text

Performance (experimental) Feature: Reusable Testcontainers

Slide 41

Slide 41 text

Performance (experimental) Feature: Reusable Testcontainers Neo4jContainer> container = new Neo4jContainer<>("neo4j:5") .withAdminPassword("verysecret") .withReuse(true)

Slide 42

Slide 42 text

Performance (experimental) Feature: Reusable Testcontainers testcontainers.reuse.enable=true ~/.testcontainers.properties Neo4jContainer> container = new Neo4jContainer<>("neo4j:5") .withAdminPassword("verysecret") .withReuse(true)

Slide 43

Slide 43 text

Conclusion ● Reproducible and portable tests ● No manual lifecycle management (JUnit extension) ● For more performance: reusable containers ● Covers more than just Neo4j ● Create Test Matrix with ease

Slide 44

Slide 44 text

44 Create data Neo4j-Migrations Neo4j Migrations: The Lean Way of Applying Database Refactorings to Neo4j Michael Simons 15:40 (UTC) 16:40 (CET)

Slide 45

Slide 45 text

45 ● Testcontainers: https://www.testcontainers.org/ ● Reusable Testcontainers: ● https://foojay.io/today/faster-integration-tests- with-reusable-testcontainers/ ● me: @meistermeier(@mastodon.social) Thank you! ❤