Main Tutorials

Spring Boot @ServiceConnection Example

Spring Boot 3.1 introduced @ServiceConnection to enable the Spring Boot’s auto-configuration to use the service connection details to connect to a remote service (Testcontainers).

Tables of contents:

1. Testing with @DynamicPropertySource

The following example uses @DynamicPropertySource to register the dynamic property values (data source connection details) to a PostgreSQL container.

BookRepositoryServiceConnectionTest.java

import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;

@SpringBootTest
@Testcontainers
public class BookRepositoryServiceConnectionTest {

  @Autowired
  private BookRepository bookRepository;

  @Container
  static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
          "postgres:15-alpine"
  );

  // Before Spring Boot 3.1 we use @DynamicPropertySource to register dynamic property values
  @DynamicPropertySource
  static void configureProperties(DynamicPropertyRegistry registry) {
      registry.add("spring.datasource.url", postgres::getJdbcUrl);
      registry.add("spring.datasource.username", postgres::getUsername);
      registry.add("spring.datasource.password", postgres::getPassword);
  }

}

2. Spring Boot 3.1 and @ServiceConnection

We must add spring-boot-testcontainers as a test dependency to use the @ServiceConnection.

pom.xml

  <!-- Spring Boot 3.1 and @ServiceConnection -->
   <dependency>
       <groupId>org.springframework.boot</groupId>
       <artifactId>spring-boot-testcontainers</artifactId>
       <scope>test</scope>
   </dependency>

The following example uses @ServiceConnection instead of @DynamicPropertySource to register the dynamic property values (data source connection details) to a PostgreSQL container.

BookRepositoryServiceConnectionTest.java


import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

@SpringBootTest
@Testcontainers
public class BookRepositoryServiceConnectionTest {

  @Autowired
  private BookRepository bookRepository;

  @Container
  // When using Testcontainers, connection details can be automatically created
  // for a service running in a container
  @ServiceConnection
  static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>(
          "postgres:15-alpine"
  );

  // With Spring Boot 3.1 and @ServiceConnection, no need this @DynamicPropertySource
  /*@DynamicPropertySource
  static void configureProperties(DynamicPropertyRegistry registry) {
      registry.add("spring.datasource.url", postgres::getJdbcUrl);
      registry.add("spring.datasource.username", postgres::getUsername);
      registry.add("spring.datasource.password", postgres::getPassword);
  }*/

}

3. Download Source Code

$ git clone https://github.com/mkyong/spring-boot.git

$ cd spring-boot-testcontainers

$ ./mvnw test -Dtest=BookRepositoryServiceConnectionTest

4. References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments