【发布时间】:2021-07-27 14:14:34
【问题描述】:
我正在尝试执行几个之前运行良好的测试用例。我最近在下面添加了一个配置类:
@Configuration
public class DBConfig {
@Value("${spring.datasource.username:}")
private String username;
@Value("${spring.datasource.url:}")
private String url;
@Bean
@DependsOn("passwordProvider")
public DataSource datasource(PasswordProvider passwordProvider) {
return DataSourceBuilder
.create()
.url(url)
.username(username)
.password(new String(passwordProvider.getKeyByName("DB_PASSWORD").getKey()))
.build();
}
}
现在,我所有的 SpringbootTests 都失败了,但有以下例外:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cloud.autoconfigure.RefreshAutoConfiguration$JpaInvokerConfiguration': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.boot.autoconfigure.jdbc.DataSourceInitializerInvoker': Invocation of init method failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'datasource' defined in class path resource [com/example/configuration/DBConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'datasource' threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1778)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:769)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:761)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:123)
知道如何修复它吗?我不想在 test.properties 中提供 passwordProvider 配置。
【问题讨论】:
-
正如您自己的堆栈所说,
Factory method 'datasource' threw exception; nested exception is java.lang.NullPointerException。您的 bean 依赖于passwordProvider,但您没有在@Configuration中显示任何具有该名称的 bean,因此可能是您没有初始化它,因此在passwordProvider.getKeyByName(...)上有一个空指针。无论如何,无法说出您在问题中提供的内容,我们只能猜测。 -
@MatteoNNZ passwordProvider bean 是通过在属性文件中提供配置来创建的。对于测试课程,我认为我不应该那样做。我只是想执行测试用例而不需要创建数据源。
-
在这种情况下,您需要一个测试配置文件,它以不同于生产的方式构建配置(例如不创建 bean,或创建模拟 bean)。查看更多about Spring profiles
-
看来您正在重新声明内置配置。让
DataSourceAutoConfiguration完成它的工作。
标签: java spring-boot unit-testing