【问题标题】:Testcontainers and Spring Boot 1.5测试容器和 Spring Boot 1.5
【发布时间】:2019-09-16 05:51:01
【问题描述】:

我们仍在使用 Spring Boot 1.5.x,我们想开始使用 TestContainers。但是,所有示例都使用 Spring boot 2.x,它使用仅在 2.x 中可用的 TestPropertyValues 类。甚至可以在 1.5.x 中将新的属性值应用到可配置的上下文中吗?

这是在 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());
        }
    }

}

【问题讨论】:

    标签: spring-boot testcontainers


    【解决方案1】:

    好问题:)。您有不同的选项来使用 Spring Boot 1.5 + TestContainers 设置您的测试上下文。您可以使用以下选项:

    通过@TestConfiguration 提供DataSource Bean

    @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();
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      只需使用JDBC URL scheme 即可启动数据库容器:

      application.properties

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

      注意: 测试容器需要在运行时位于应用程序的类路径中才能正常工作

      【讨论】:

        猜你喜欢
        • 2021-10-19
        • 2015-04-15
        • 2017-11-07
        • 2019-01-27
        • 2018-01-16
        • 2021-08-28
        • 2018-01-02
        • 2018-06-09
        • 2016-03-15
        相关资源
        最近更新 更多