"No suitable driver" 在 Spring 单元测试中
"No suitable driver" in Spring unit test
我想在我的 Spring 项目中编写单元测试。
整个项目只有一个应用配置。
应用程序 运行 很好,但我在单元测试中收到“由 java.sql.SQLException 引起:没有合适的驱动程序”异常。
如何将驱动程序设置为测试?
单元测试中唯一使用的注解:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
application.yml中的数据库连接设置:
spring:
main:
allow-bean-definition-overriding: false
application:
name: application
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.sybase.jdbc4.jdbc.SybDriver
password: xxx
url: jdbc:sybase:Tds:localhost:3268/my_db
username: admin
hikari:
connection-test-query: SELECT 1
liquibase:
change-log: liquibase/changelog.sql
user: admin
password: xxx
jpa:
database: SYBASE
database-platform: org.hibernate.dialect.SybaseAnywhereDialect
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
终于找到原因了
由于我在测试文件夹中添加了一个测试配置class,额外的配置也被加载到单个注释“@SpringBootTest”测试中,数据库连接设置失败。
当我在额外的测试配置class文件中注释@TestConfiguration时,单个@SpringBootTest的测试可以正常工作。
我想在我的 Spring 项目中编写单元测试。 整个项目只有一个应用配置。 应用程序 运行 很好,但我在单元测试中收到“由 java.sql.SQLException 引起:没有合适的驱动程序”异常。
如何将驱动程序设置为测试?
单元测试中唯一使用的注解:
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
application.yml中的数据库连接设置:
spring:
main:
allow-bean-definition-overriding: false
application:
name: application
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.sybase.jdbc4.jdbc.SybDriver
password: xxx
url: jdbc:sybase:Tds:localhost:3268/my_db
username: admin
hikari:
connection-test-query: SELECT 1
liquibase:
change-log: liquibase/changelog.sql
user: admin
password: xxx
jpa:
database: SYBASE
database-platform: org.hibernate.dialect.SybaseAnywhereDialect
hibernate:
ddl-auto: none
naming:
physical-strategy: org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
终于找到原因了
由于我在测试文件夹中添加了一个测试配置class,额外的配置也被加载到单个注释“@SpringBootTest”测试中,数据库连接设置失败。
当我在额外的测试配置class文件中注释@TestConfiguration时,单个@SpringBootTest的测试可以正常工作。