Search code examples
javaspringspring-boot-test

"No suitable driver" in Spring unit test


I want to write unit test in my Spring project. There is only one application config in the whole project. The application run well, but I get a "Caused by: java.sql.SQLException: No suitable driver" exception in unit test.

How can I setup a driver into the test?

The only annotation used in unit test:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

The db connection settings in 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

Solution

  • I finally find out the reason.

    Since I had added a test configuration class into test folder, the extra configuration is also loaded into the single annotation "@SpringBootTest" test and the db connection setup fail.

    When I comment the @TestConfiguration in the extra test configuration class file, the test with single @SpringBootTest can work normally.