I tried to taste the ImportTestContainers
feature in Spring Boot 3.1. All my attempts are failed.
I have added the spring-boot-testcontaienrs to test scope in my project, and created a simple test to use ImportTestcontainers
.
@SpringBootTest
@Import(ImportTestcontainersExampleTests.MyTestConfig.class)
@Slf4j
public class ImportTestcontainersExampleTests {
@TestConfiguration(proxyBeanMethods = false)
@ImportTestcontainers(MyContainers.class)
static class MyTestConfig {
}
// interface MyContainers {
// @Container
// PostgreSQLContainer<?> PG_CONTAINER = new PostgreSQLContainer<>(DockerImageName.parse("postgres:14"));
// }
class MyContainers {
@Container
static PostgreSQLContainer<?> PG_CONTAINER = new PostgreSQLContainer<>(DockerImageName.parse("postgres:14"));
}
// class MyContainers {
//
// PostgreSQLContainer postgreSQLContainer(DynamicPropertyRegistry registry) {
// PostgreSQLContainer<?> PG_CONTAINER = new PostgreSQLContainer<>(DockerImageName.parse("postgres:14"));
//
// registry.add("spring.r2dbc.url", () -> "r2dbc:postgresql://" + PG_CONTAINER.getHost() + ":" + PG_CONTAINER.getFirstMappedPort() + "/" + PG_CONTAINER.getDatabaseName());
// registry.add("spring.r2dbc.username", PG_CONTAINER::getUsername);
// registry.add("spring.r2dbc.password", PG_CONTAINER::getPassword);
//
// return PG_CONTAINER;
// }
// }
@Autowired
private ProductRepository productRepository;
@Test
public void testProductRepository() {
var product = productRepository.save(new Product(null, "test", BigDecimal.ONE));
product.as(StepVerifier::create)
.consumeNextWith(p -> {
assertThat(p).isNotNull();
assertThat(p.id()).isNotNull();
})
.verifyComplete();
}
}
In this test, I have tried declares containers via an interface field, or static fields in a class or method return type Container with DynamicPropertyRegistry
method parameter in a class, none of these are worked.
The source codes is hosted on my Github: https://github.com/hantsy/spring6-sandbox/tree/master/boot-data-r2dbc