Skip to content

@DataJpaTest test slice doesn't working with container created in @TestConfiguration #36239

Closed as not planned
@piotrooo

Description

@piotrooo

For the following containers configuration:

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfiguration {
    @Bean
    @ServiceConnection
    @RestartScope
    PostgreSQLContainer<?> postgresContainer() {
        return new PostgreSQLContainer<>("postgres:13");
    }

    @Bean
    @ServiceConnection
    @RestartScope
    RabbitMQContainer rabbitContainer() {
        return new RabbitMQContainer("rabbitmq:3.11");
    }
}

When I try to use it in the repository tests (which are using a slice test annotation @DataJpaTest) containers aren't reused and configured correctly.

@DataJpaTest
@Testcontainers
@Import(ContainersConfiguration.class)
@AutoConfigureTestDatabase(replace = NONE)
class RepositoryItTest {
    // tests
}

Declare properties in the @DataJpaTest works (creates only slice of the context without rabbit), but that seems to be a duplication.

@DataJpaTest(properties = {
        "spring.datasource.url=jdbc:p6spy:tc:postgresql:13://service-test"
})
@Testcontainers
@AutoConfigureTestDatabase(replace = NONE)
class RepositoryItTest {
}

Using an annotation @SpringBootTest, make this test working but it's create a whole context, which containers (rabbit) which aren't necessary for run them.

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Testcontainers
@Import(ContainersConfiguration.class)
@AutoConfigureTestDatabase(replace = NONE)
class RepositoryItTest {
    // tests
}

Spring Boot: 3.1.1

Possible related with #35253.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions