Spring Data Jpa Sping Boot 3.1 with compose and postgres dependencies fails running contextLoad test

zc0qhyus  于 11个月前  发布在  Spring
关注(0)|答案(1)|浏览(172)

我刚刚创建了一个Spring Boot 3.1.3项目,依赖项如下:

  • Web
  • Data JPA
  • 测试
  • Docker Compose
  • Postgres
  • 测试容器

它包括一个compose.yml文件与postgres图像的初始设置。我运行应用程序和所有罚款!
然后我运行mvn clean test命令,在运行contextLoad()测试时出现此错误:

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

字符串
这是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


我应该在测试中排除自动配置数据源吗?或者有新的方法来处理数据库连接配置?

k3fezbri

k3fezbri1#

我为此报告了一个issue,Sping Boot 团队通过以下评论解决了这个问题:
这是意料之中的,因为在运行测试时,Docker Compose支持默认情况下是禁用的,并且由https://start.spring.io生成的测试还不是configured to use Testcontainers
作为一种变通方法,我只是添加了一个H2依赖项,如下所示:

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

字符串
这样,Spring Boot就不会抛出任何异常。

相关问题