【问题标题】:Spring boot unit test with h2 failing for schema带有 h2 的 Spring Boot 单元测试失败的模式
【发布时间】:2021-06-14 05:37:49
【问题描述】:

我有如下插入语句的 sql 文件

插入 demo.course (col1, col2) VALUES('Val 1', 'KS1')

我的单元测试

@ExtendWith(SpringExtension.class)
@DataJpaTest
@AutoConfigureTestDatabase //h2
@ActiveProfiles("test")
class TopicRepositoryTest extends BaseRepositoryTest {

    @Test
    public void newTopicCreationShouldBePossible() {        
        Topics newTopic = createTopics("STEAM"); //from base
        assertNotNull(newTopic.getId());
    }
}

Application-test.props

spring.datasource.url=jdbc:h2:mem:testdb;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false

spring.jpa.properties.hibernate.default_schema=DEMO

spring.datasource.hikari.schema=DEMO

错误:

引起:org.springframework.beans.factory.BeanCreationException: 创建类中定义的名称为“entityManagerFactory”的bean时出错 路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: bean初始化失败;嵌套异常是 org.springframework.jdbc.datasource.init.ScriptStatementFailedException: 执行类路径资源的 SQL 脚本语句 #1 失败 [maria/importcommon.sql]: 插入 demo.mytable (col1, col2) 值('VAL 1','KS1');嵌套异常是 org.h2.jdbc.JdbcSQLSyntaxErrorException:找不到模式“DEMO”; SQL 声明:

阅读一些帖子,我添加了以下内容,但它不起作用

spring.jpa.properties.hibernate.default_schema=DEMO

spring.datasource.hikari.schema=DEMO

如何解决此问题并通过测试?

【问题讨论】:

    标签: spring-boot junit h2


    【解决方案1】:

    如果不存在,您可以使用 spring.datasource.url 创建架构

    spring.datasource.url=jdbc:h2:mem:testdb;INIT=CREATE SCHEMA IF NOT EXISTS DEMO;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;DATABASE_TO_UPPER=false
    

    【讨论】:

      【解决方案2】:

      我建议在没有架构前缀的情况下更新您的插入语句

      插入课程 (col1, col2) VALUES('Val 1', 'KS1');

      删除了“演示”。

      【讨论】:

      • 我看到一个错误 - Schema not found sql 语句。 app.property spring.jpa.properties.hibernate.default_schema=demo 干扰了 application-test.properties。那么有没有办法使该架构不存在?
      • 如果我没有在测试配置文件属性 spring.jpa.properties.hibernate.default_schema= 中指定为空,则可以使用
      猜你喜欢
      • 2016-02-09
      • 2018-12-13
      • 1970-01-01
      • 2022-06-29
      • 1970-01-01
      • 2018-10-24
      • 2019-02-04
      • 2018-02-05
      • 2016-05-28
      相关资源
      最近更新 更多