如何在 Spring Data R2DBC 中运行类似 @Sql 的查询进行测试 [英] How to run query like @Sql for test in Spring Data R2DBC

查看:65
本文介绍了如何在 Spring Data R2DBC 中运行类似 @Sql 的查询进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a Repository and writing a Test Case. I want to run insert.sql before executing the test method with @Sql (org.springframework.test.context.jdbc) but it doesn't run. Is there another way?

推荐答案

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

I faced the same issue, Spring Data R2DBC doesn't support @SQl annotation yet.

使用下面的函数:

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

application.yml:

application.yml:

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

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

When connection pool in used in tests, we should close every created connection because it seems to be not released. Example, if we have 16 pool size, we'll be able to execute only 16 tests then get an infinite loop started from 17th.

测试:

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

这篇关于如何在 Spring Data R2DBC 中运行类似 @Sql 的查询进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆