java - 如何在Spring Data R2DBC中运行@Sql之类的查询进行测试

标签 java spring spring-data spring-data-r2dbc

我创建了一个存储库并编写了一个测试用例。我想在使用@Sql (org.springframework.test.context.jdbc) 执行测试方法之前运行insert.sql 但它没有运行。还有别的办法吗?

最佳答案

我遇到了同样的问题,Spring Data R2DBC 还不支持@SQl 注解。

使用下面的函数:

    private void executeSqlScriptBlocking(final Resource sqlScript) {
        Mono.from(connectionFactory.create())
                .flatMap(connection -> ScriptUtils.executeSqlScript(connection, sqlScript)
                        .then(Mono.from(connection.close())))
                .block();
    }

然后:

    @Autowired
    private ConnectionFactory connectionFactory;

    @BeforeEach
    void init(@Value("classpath:init_test_data.sql") Resource sqlScript) {
        executeSqlScriptBlocking(sqlScript);
    }

应用程序.yml:

spring:
  r2dbc:
    pool:
      enabled: true
      initial-size: 8
      max-size: 16

当在测试中使用连接池时,我们应该关闭每一个创建的连接,因为它似乎没有被释放。例如,如果我们有 16 个池大小,我们将只能执行 16 个测试,然后从第 17 个开始无限循环。

测试于:

Java: 8(jdk-8.0.272.10-hotspot)
Spring Boot: 2.3.9
JUnit 5: 5.6.3

关于java - 如何在Spring Data R2DBC中运行@Sql之类的查询进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61692151/

相关文章:

java - 无法打开 JPA EntityManager 进行事务。 JTA EntityManager 无法使用 getTransaction()

java - 访问子类中的隐藏字段

java.lang.ClassCastException : java. lang.String 无法转换为 [Ljava.lang.Object;当尝试通过 Hibernate 将列值获取到列表时

spring - 使用 Spring Security 配置 HTTP 页面和 HTTPS 页面

java - Spring Data JPA 和 Projection 为 TupleConverter 获取 ConverterNotFoundException

Spring Data JPA PageRequest 按 Map 值属性排序

java - 为什么 JPA + TransactionManagementConfigurer 不起作用

javascript - 如何使用 Nashorn 将 Javascript 对象从 Java 传递到 Javascript

java - Openjdk 和 Java webstart

java - 如何在 Swing 中从多个 JList 中仅选择一项