2

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

Hantsy
  • 8,006
  • 7
  • 64
  • 109

1 Answers1

1

@Import(ImportTestcontainersExampleTests.MyTestConfig.class) is not needed due to MyTestConfig class is annotated with TestConfiguration and already in the test class. Also, missing the @ServiceConnection for PostgreSQLContainer. @ServiceConnection is how spring boot will autoconfigure the configuration properties.

Last but not least, there is no need to use testcontainers-bom anymore because spring-boot already manage those dependencies, starting from 3.1.0.

Eddú Meléndez
  • 6,107
  • 1
  • 26
  • 35
  • BTW, I followed [the official doc](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.testing.testcontainers.at-development-time.dynamic-properties), create another example test and [add container as beans](https://github.com/hantsy/spring6-sandbox/blob/master/boot-data-r2dbc/src/test/java/com/example/demo/TestcontainersBeanExampleTests.java#L36-L45), it failed. – Hantsy May 23 '23 at 14:17
  • I think the docs are ok because there is a tip at the end https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.testing.testcontainers.at-development-time.dynamic-properties – Eddú Meléndez May 23 '23 at 16:50
  • TestcontainersBeanExampleTests seems to work for me. – Eddú Meléndez May 23 '23 at 16:50
  • but [the alternative approach(commented codes)](https://github.com/hantsy/spring6-sandbox/blob/master/boot-data-r2dbc/src/test/java/com/example/demo/TestcontainersBeanExampleTests.java#L36-L45) does not work. – Hantsy May 24 '23 at 03:08
  • It could be a bug, I just checked the source code and it completely relies on connectiondetails https://github.com/spring-projects/spring-boot/blob/main/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/r2dbc/ConnectionFactoryOptionsInitializer.java#L50-L52 I think it should check connection details, fallback with properties and then embedded. Not sure if it is intended. – Eddú Meléndez May 24 '23 at 15:23