【发布时间】:2021-09-20 09:20:17
【问题描述】:
我的 spring boot 测试在 maven 构建期间抛出以下错误。
"level":"ERROR","logger":"com.zaxxer.hikari.HikariConfig","msg":"HikariPool-1 - jdbcUrl is required with driverClassName."
堆栈跟踪:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'schedulerFactoryBean' defined in class path resource
[com/pit/SchedulerConfig.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: jdbcUrl is requir
ed with driverClassName.
Caused by: java.lang.IllegalArgumentException: jdbcUrl is required with driverClassName.
在 MyApplicationTest.java 中引发错误的方法
@SpringBootTest
@EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class,
DataSourceTransactionManagerAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
@EnableConfigurationProperties(value = MyConfig.class)
class MyApplicationTest {
@Test
void contextLoads() {
assertNotNull(myController);
}
}
我的 pom 在类路径中有这些依赖项。
- spring-boot-starter-web
- spring-boot-starter-test
- spring-integration-sftp
- postgresql
- junit
- 石英调度器
我的数据源 bean 配置如下
@Configuration
public class MyDataSourceCfg {
private DataSource appDataSource() {
return DataSourceBuilder.create()
.driverClassName("org.postgresql.Driver")
.url(env.getProperty("url") // values are set from Environment
.username(env.getProperty("user"))
.password(env.getProperty("password")
.build();
}
}
请告诉我发生了什么以及如何避免此错误。如果您有多个数据库但我只有一个 postgres 数据库,我阅读了有关以不同方式配置数据源的信息。请分享您的想法。
【问题讨论】:
-
.url(env.getProperty("url")似乎没有 JDBC URL “jdcb: ...”。错误通常位于带有注释的行。 -
@JoopEggen 属性从保险库中提取并在启动期间放入环境中。 “jdbc:postgresql://...” 是我配置它的方式。如果我在构建期间跳过测试,我可以启动应用程序并运行。
-
也许单元测试有不同的配置? Spring 通常有一个应用程序上下文和一个应用程序上下文测试。并且测试通常针对不同的数据库。
-
[ERROR] contextLoads Time elapsed: 0 s <<< ERROR! java.lang.IllegalStateException: Failed to load ApplicationContext我在堆栈错误上方看到的内容。已将测试类放在问题中。除了一个其他自动装配的 bean 之外,该类中没有其他内容。 -
抱歉,我帮不上忙。您可以从硬编码字符串开始:
.url("jdbc: ..."),然后进行日志记录。
标签: java spring spring-boot