1

I just created a Spring Boot 3.1.3 project with dependencies as below:

  • Web
  • Data JPA
  • Test
  • Docker compose
  • Postgres
  • Testcontainers

It includes a compose.yml file with initial settings for postgres image. I run the application and all fine!

Then I run mvn clean test command and I get this error while running contextLoad() test:

[ERROR] com.example.demo.DemoApplicationTests.contextLoads  Time elapsed: 0.028 s  <<< ERROR!

and this is stack-trace:

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception with message: Failed to determine a suitable driver class
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:171)
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:655)
        ... 113 more
Caused by: org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
        at org.springframework.boot.autoconfigure.jdbc.DataSourceProperties.determineDriverClassName(DataSourceProperties.java:186)
        at org.springframework.boot.autoconfigure.jdbc.PropertiesJdbcConnectionDetails.getDriverClassName(PropertiesJdbcConnectionDetails.java:49)
        at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration.createDataSource(DataSourceConfiguration.java:55)
        at org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari.dataSource(DataSourceConfiguration.java:117)
        at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
        at java.base/java.lang.reflect.Method.invoke(Method.java:578)
        at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139)
        ... 114 more

Should i exclude auto-configuring the data source in the tests? Or there are new ways of handling database connection configurations?

4
  • The error message says it couldn’t find a suitable driver class. Do you’ve a dependency on the database driver? Commented Sep 10, 2023 at 10:22
  • I know how to fix this in boot versions before 3.1. There are a lot additions in this new version for docker and testcontainers and I guess there is way to do it in a new way. Commented Sep 10, 2023 at 10:42
  • The fix has always been the same,, and It’s got nothing to do with Spring at all. add a dependency to the driver in your build file. Commented Sep 10, 2023 at 11:10
  • Can you maybe submit at least pom.xml and application.yaml files? Commented Sep 10, 2023 at 13:22

1 Answer 1

1

I reported an issue for this and Spring boot team closed that issue with this comment:

This is to be expected as the Docker Compose support is disabled by default when running tests and the test that is generated by https://start.spring.io isn't yet configured to use Testcontainers.

As a workaround, I just added an H2 dependency as below:

<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>

This way, Spring Boot will not throw any exception.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.