Mejmo
Mejmo

Reputation: 2593

Testcontainers and Spring Boot 1.5

We are still using Spring Boot 1.5.x and we want to start using TestContainers. However, all examples are with Spring boot 2.x which is using TestPropertyValues class only available in 2.x. Is it even possible to apply new property values to the configurable context in 1.5.x?

This is the code working in 2.x:

@RunWith(SpringRunner.class)
@SpringBootTest
@ContextConfiguration(initializers = {UserRepositoryTCIntegrationTest.Initializer.class})
public class UserRepositoryTCIntegrationTest extends UserRepositoryCommonIntegrationTests {

    @ClassRule
    public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer("postgres:11.1")
      .withDatabaseName("integration-tests-db")
      .withUsername("sa")
      .withPassword("sa");

    static class Initializer
      implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            TestPropertyValues.of(
              "spring.datasource.url=" + postgreSQLContainer.getJdbcUrl(),
              "spring.datasource.username=" + postgreSQLContainer.getUsername(),
              "spring.datasource.password=" + postgreSQLContainer.getPassword()
            ).applyTo(configurableApplicationContext.getEnvironment());
        }
    }

}

Upvotes: 1

Views: 1697

Answers (2)

timomeinen
timomeinen

Reputation: 3320

Database containers can be launched by simply using a JDBC URL scheme:

application.properties

spring.datasource.driver-class-name=org.testcontainers.jdbc.ContainerDatabaseDriver
spring.datasource.url=jdbc:tc:postgresql:11://localhost/test

Note: Testcontainers needs to be on your application's classpath at runtime for this to work

Upvotes: 0

judomu
judomu

Reputation: 538

good question :). You have different options to setup your testcontext with Spring Boot 1.5 + TestContainers. Instead of using an indirect way by setting the datasource-properties with dynamic values (like in your example code), you can use the following option:

Provide DataSource Bean via @TestConfiguration

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
public class YourRepositoryIntTest {

    @Autowired
    private YourRepository sut;

    @Test
    public void testMethod() {
        // Given
        String expectedId = "SOMEID";

        // When
        Entity entity = sut.testMethod();

        // Then
        Assertions.assertThat(entity.getId()).isEqualTo(expectedId);
    }


    @TestConfiguration
    public static class Config {
        @Bean
        public MySQLContainer testContainer() {
            MySQLContainer container = new MySQLContainer();
            container.start();

            return container;
        }

        @Bean
        @Primary
        public DataSource dataSource(MySQLContainer container) {
            return DataSourceBuilder.create()
                    .url(container.getJdbcUrl())
                    .username(container.getUsername())
                    .password(container.getPassword())
                    .driverClassName(container.getDriverClassName())
                    .build();
        }
    }
}

Upvotes: 2

Related Questions