Containerhafen symbolisch für Docker Container
Simon, Kiya | 14.07.2023

Integrating Testcontainers, Postgres, Postgis with Spring and Spock

Webentwicklung > Integrating Testcontainers, Postgres, Postgis with Spring and Spock

Testcontainers provides a brilliant solution to include all the essentials for integration tests. With this strategy, you can easily initiate docker containers directly from your test code. This eliminates the need to write test docker-compose.yml files and document README.md code instructing how to even initiate a test.

Below, you will find a concise example depicting how to work with testcontainers using Spring and Spock on a Postgis / Postgres database.

1@SpringBootTest
2@AutoConfigureMockMvc
3@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
4@ContextConfiguration(initializers = [Initializer.class])
5class FieldControllerTest extends IntegrationTest {
6
7  @Shared
8  public static JdbcDatabaseContainer postgreSQLContainer = new PostgisContainerProvider()
9    .newInstance()
10    .withDatabaseName("integration-tests-db")
11    .withUsername("sa")
12    .withPassword("sa");
13
14
15  static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
16
17    void initialize(ConfigurableApplicationContext configurableApplicationContext) {
18      postgreSQLContainer.start()
19      TestPropertyValues.of(
20        "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
21        "spring.datasource.username=" + postgreSQLContainer.getUsername(),
22        "spring.datasource.password=" + postgreSQLContainer.getPassword()
23      ).applyTo(configurableApplicationContext.getEnvironment());
24    }
25  }
26
27  @Autowired
28  MockMvc mockMvc
29
30  @Autowired
31  FieldRepository fieldRepository
32
33  void setup() {
34    fieldRepository.save(new Field(
35      UUID.fromString('ad68f894-c16b-4953-b577-7cddb3e85ae5'), "initSampleField",
36      new Polygon(
37        PositionSequenceBuilders.variableSized(G2D.class)
38          .add(5.8208837124389, 51.0596004663904)
39          .add(5.83490292265498, 51.0571257015788)
40          .add(5.87078646658134, 51.0451607414904)
41          .add(5.79146302423308, 51.0612386272784)
42          .add(5.8208837124389, 51.0596004663904)
43          .toPositionSequence(),
44        CoordinateReferenceSystems.WGS84
45      )
46    ))
47  }
48
49  ...
50}

To successfully integrate testcontainers into your project, you need the following dependencies:

1dependencyManagement {
2  imports {
3    mavenBom "org.testcontainers:testcontainers-bom:${testcontainersVersion}"
4  }
5}
6
7dependencies {
8  ...
9  
10  testImplementation "org.testcontainers:spock:1.17.5"
11  testImplementation "org.testcontainers:postgresql:1.17.5"
12}

Happy testing 🎉

Inhalt
  • How to use Testcontainers for integration tests?
  • How does Testcontainers work with Spring and Spock?
  • What are the necessary dependencies for integrating Testcontainers into your project?
Simon Jakubowski
Simon (Softwareentwickler)

… ist erfahrener Software-Architekt, Product Owner und Backend-Entwickler in Hannover. Er betreut mehrere Projekte als Tech Lead und unterstützt unsere Kunden bei der Anforderungsanalyse sowie der Pro... mehr anzeigen

Github
Gesicht von Kiya,- unsere KI Mitarbeiterin
Kiya

... ist unsere engagierte und leidenschaftliche Künstliche Intelligenz und Expertin für Softwareentwicklung. Mit einem unermüdlichen Interesse für technologische Innovationen bringt sie Enthusiasmus u... mehr anzeigen

More about this topic

More from Simon

Unsere Entwicklungsexpertise

Standort Hannover

newcubator GmbH
Bödekerstraße 22
30161 Hannover

Standort Dortmund

newcubator GmbH
Westenhellweg 85-89
44137 Dortmund