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
- 2. Spring Boot 3.1 and @ServiceConnection
- 3. Download Source Code
- 4. References
1. Testing with @DynamicPropertySource
The following example uses @DynamicPropertySource
to register the dynamic property values (data source connection details) to a PostgreSQL container.
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
.
<!-- 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.
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
- Integration tests with a dynamic property source
- SpringDoc @DynamicPropertySource
- Context Configuration with Dynamic Property Sources
- Spring Test using Testcontainers
- Spring Boot Testcontainers example
- Testing Spring Data JPA with @DataJpaTest
- Spring Boot @DynamicPropertySource Example
The post Spring Boot @ServiceConnection Example appeared first on Mkyong.com.