Spring Boot 2.0.4-H2数据库-@SpringBootTest-失败原因:org.h2.jdbc.JdbcSQLException:找不到架构“ classpath:db / schema.sql”

时间:2018-08-18 08:56:41

标签: spring-boot spring-data-jpa h2

我已经使用Spring Boot 2.0.4构建了一个应用程序,并与JPA集成。

现在我想使用h2数据库运行一些测试,但是我无法使用h2执行Spring Boot测试。

例外显示如下:

Caused by: org.springframework.jdbc.datasource.init.UncategorizedScriptException: Failed to execute database script; nested exception is org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:58)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.runScripts(DataSourceInitializer.java:210)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializer.createSchema(DataSourceInitializer.java:104)
    at org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker.afterPropertiesSet(DataSourceInitializerInvoker.java:64)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1758)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1695)
    ... 74 more
Caused by: org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obtain JDBC Connection; nested exception is org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:81)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:46)
    ... 79 more
Caused by: org.h2.jdbc.JdbcSQLException: Schema "classpath:db/schema.sql" not found [90079-197]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:357)
    at org.h2.message.DbException.get(DbException.java:179)
    at org.h2.message.DbException.get(DbException.java:155)
    at org.h2.engine.Database.getSchema(Database.java:1808)
    at org.h2.engine.Session.setCurrentSchemaName(Session.java:1317)
    at org.h2.jdbc.JdbcConnection.setSchema(JdbcConnection.java:1989)
    at com.zaxxer.hikari.pool.PoolBase.setupConnection(PoolBase.java:423)
    at com.zaxxer.hikari.pool.PoolBase.newConnection(PoolBase.java:370)
    at com.zaxxer.hikari.pool.PoolBase.newPoolEntry(PoolBase.java:194)
    at com.zaxxer.hikari.pool.HikariPool.createPoolEntry(HikariPool.java:460)
    at com.zaxxer.hikari.pool.HikariPool.checkFailFast(HikariPool.java:534)
    at com.zaxxer.hikari.pool.HikariPool.<init>(HikariPool.java:115)
    at com.zaxxer.hikari.HikariDataSource.getConnection(HikariDataSource.java:112)
    at org.springframework.jdbc.datasource.DataSourceUtils.fetchConnection(DataSourceUtils.java:151)
    at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:115)
    at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:78)
    ... 80 more

我的application-test.properties是:

spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.jdbc-url=jdbc:h2:mem:test
spring.datasource.schema=classpath:db/schema.sql
spring.datasource.data=classpath:db/data.sql
spring.datasource.initialization-mode=always


spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=none
logging.level.org.hibernate.SQL=debug
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

这些异常在我执行schema.sql时发生,但是h2希望在spring.datasource.schema中指定的名为“ classpath:schema.sql”的架构中执行此脚本。

2 个答案:

答案 0 :(得分:1)

通过“未找到”异常很清楚,Spring引导无法找到schema.sql文件。

只需将src/main/resources/db/schema.sql复制到src/test/resources/db/schema.sql

答案 1 :(得分:1)

您的媒体资源应如下所示:

spring.datasource.schema=db/schema.sql

在DataSourceInitializer类中,您可以看到它的用法

public boolean createSchema() {
    List<Resource> scripts = getScripts("spring.datasource.schema",
            this.properties.getSchema(), "schema");
    if (!scripts.isEmpty()) {
        if (!isEnabled()) {
            logger.debug("Initialization disabled (not running DDL scripts)");
            return false;
        }
        String username = this.properties.getSchemaUsername();
        String password = this.properties.getSchemaPassword();
        runScripts(scripts, username, password);
    }
    return !scripts.isEmpty();
}

getScripts方法添加类路径字符串:

private List<Resource> getScripts(String propertyName, List<String> resources,
        String fallback) {
    if (resources != null) {
        return getResources(propertyName, resources, true);
    }
    String platform = this.properties.getPlatform();
    List<String> fallbackResources = new ArrayList<>();
    fallbackResources.add("classpath*:" + fallback + "-" + platform + ".sql");
    fallbackResources.add("classpath*:" + fallback + ".sql");
    return getResources(propertyName, fallbackResources, false);
}

因此您不必进行设置。