2

我正在尝试执行以下操作;
但是使用org.testcontainers.containers.MySQLR2DBCDatabaseContainer
谁能告诉我这是如何实现的,因为 MySQLR2DBCDatabaseContainer 似乎没有以下方法:

  • ::getJdbcUrl
  • ::获取密码
  • ::获取用户名
@Testcontainers
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
public class ApplicationIT {
  @Container
  public static PostgreSQLContainer postgreSQLContainer = new PostgreSQLContainer()
    .withPassword("inmemory")
    .withUsername("inmemory");
  @DynamicPropertySource
  static void postgresqlProperties(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url", postgreSQLContainer::getJdbcUrl);
    registry.add("spring.datasource.password", postgreSQLContainer::getPassword);
    registry.add("spring.datasource.username", postgreSQLContainer::getUsername);
  }
  @Test
  public void contextLoads() {
  }
}
4

2 回答 2

4

对于您的情况,您应该为 R2dbc 连接而不是 Jdbc 连接创建属性。

您必须手动计算 R2dbc URL 而不是 Jdbc URL。

registry.add("spring.r2dbc.url", () -> "r2dbc:postgresql://"
                + postgreSQLContainer.getHost() + ":" + postgreSQLContainer.getFirstMappedPort()
                + "/" + postgreSQLContainer.getDatabaseName());
registry.add("spring.r2dbc.username", () -> postgreSQLContainer.getUsername());
registry.add("spring.r2dbc.password", () -> postgreSQLContainer.getPassword());

更简单地说,您可以为测试容器创建tc基于配置文件的应用程序配置。

# src/test/resources/application-tc.properties
spring.r2dbc.url=r2dbc:tc:mysql:///databasename?TC_IMAGE_TAG=8

url 中的tc会自动启动一个 testcontaiens docker。请参阅TestContainers R2dbc 支持

然后,您还可以通过@TestConfiguration.

@DataR2dbcTest
@TestInstance(TestInstance.Lifecycle.PER_METHOD)
@ActiveProfiles("tc")// activate the `tc` profile.
public class TestcontainersPostRepositoryTest {

    @TestConfiguration
    static class TestConfig {
        @Bean
        public ConnectionFactoryInitializer initializer(ConnectionFactory connectionFactory) {

            ConnectionFactoryInitializer initializer = new ConnectionFactoryInitializer();
            initializer.setConnectionFactory(connectionFactory);

            CompositeDatabasePopulator populator = new CompositeDatabasePopulator();
            populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("schema.sql")));
            populator.addPopulators(new ResourceDatabasePopulator(new ClassPathResource("data.sql")));
            initializer.setDatabasePopulator(populator);

            return initializer;
        }
    }

    @Autowired
    DatabaseClient client;

    @Autowired
    PostRepository posts;

//your tests...

我有一些测试容器的例子。

  1. 通过 R2dbc url 启动 Contrainer,请参阅此 R2dbc MySQL 示例

  2. 使用 Junit 手动设置,请参阅此 R2dbc PostgresSQL 示例

  3. 使用 spring 初始化器类来初始化一个容器,参见这个 Neo4j Rx 示例

于 2020-09-07T14:25:51.263 回答
0

这实际上有点棘手。如果您使用的是反应式存储库,您应该继续使用MySQLContainer,但您必须更改设置数据源的方式(我的代码略有不同,但我希望足够接近,让您了解如何解决问题)。请注意使用UserRepositoryIntegrationTest::r2dbcUrl.

@Testcontainers
@DataR2dbcTest
class UserRepositoryIntegrationTest {

    @Container
    private static MySQLContainer database = new MySQLContainer("mysql:8.0.28");

    @DynamicPropertySource
    static void registerDynamicProperties(DynamicPropertyRegistry registry) {
        registry.add("spring.r2dbc.url", UserRepositoryIntegrationTest::r2dbcUrl);
        registry.add("spring.r2dbc.username", database::getUsername);
        registry.add("spring.r2dbc.password", database::getPassword);

        registry.add("spring.flyway.url", database::getJdbcUrl);
        registry.add("spring.flyway.user", database::getUsername);
        registry.add("spring.flyway.password", database::getPassword);
    }

    private static String r2dbcUrl() {
        return String.format("r2dbc:mysql://%s:%s/%s",
                database.getContainerIpAddress(),
                database.getMappedPort(MySQLContainer.MYSQL_PORT),
                database.getDatabaseName());
    }
}

你可以看看我的repo;我几天都陷入了同样的问题:UserRepositoryIntegrationTest.java

于 2022-01-30T11:55:44.657 回答