我刚刚创建了一个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
型
我应该在测试中排除自动配置数据源吗?或者有新的方法来处理数据库连接配置?
1条答案
按热度按时间k3fezbri1#
我为此报告了一个issue,Sping Boot 团队通过以下评论解决了这个问题:
这是意料之中的,因为在运行测试时,Docker Compose支持默认情况下是禁用的,并且由https://start.spring.io生成的测试还不是configured to use Testcontainers。
作为一种变通方法,我只是添加了一个
H2
依赖项,如下所示:字符串
这样,
Spring Boot
就不会抛出任何异常。