java.sql.SQLException:驱动程序:oracle.jdbc.driver.OracleDriver为URL返回null:jdbc:h2:/ data / sample; IFEXISTS = TRUE

时间:2015-10-01 19:54:10

标签: spring maven h2

免责声明:Spring-boot noob。

我尝试使用spring-boot设置H2数据库进行集成测试。

我收到错误:java.sql.SQLException: Driver:oracle.jdbc.driver.OracleDriver@2e179f3e returned null for URL:jdbc:h2:/data/sample;IFEXISTS=TRUE

我不确定该怎么做。
我必须覆盖spring.datasource.url,因为该值应该在应用中。

FirstTest.groovy

package com.api

import groovy.util.logging.Slf4j
import org.junit.Test
import org.junit.runner.RunWith
import org.springframework.boot.test.IntegrationTest
import org.springframework.boot.test.SpringApplicationConfiguration
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = Api.class)
@IntegrationTest(value = "spring.profiles.active=h2")
@Slf4j
class FirstTest {


    @Test
    void test() {
        log.info 'debug log statement'
    }

}

的src /测试/资源/配置/ application-h2.properties

spring.datasource.dataSourceClassName=org.h2.jdbcx.JdbcDataSource
spring.datasource.url=jdbc:h2:/data/sample;IFEXISTS=TRUE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.database=H2

的pom.xml

...
<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
...
<dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.189</version>
</dependency>
...

编辑:这篇文章:spring boot default H2 jdbc connection (and H2 console)似乎回答了我的问题

的src /测试/资源/配置/ application-h2.properties

spring.datasource.url=jdbc:h2:mem:foo-database;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect

1 个答案:

答案 0 :(得分:1)

看起来您需要显式设置JDBC驱动程序类名,就像在其他定义中那样:

spring.datasource.driverClassName=org.h2.Driver // <== add this
spring.datasource.dataSourceClassName=org.h2.jdbcx.JdbcDataSource
spring.datasource.url=jdbc:h2:/data/sample;IFEXISTS=TRUE
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
spring.jpa.database=H2

另请参阅related question