1

I am trying to execute couple of test cases which were earlier working fine. I recently added a Configuration class which is below :

@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();
}
 } 

Now, all my SpringbootTests fail with the below exception:

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)

Any idea how to get it fixed ? I dont want to provide passwordProvider configuration in test.properties.

4
  • As your own stack says, Factory method 'datasource' threw exception; nested exception is java.lang.NullPointerException. Your bean depends on passwordProvider but you're not showing any bean with that name in your @Configuration, so it may be that you're not initializing it and so having a null pointer over passwordProvider.getKeyByName(...). In any case, it's impossible to say with what you're providing in the question, we can just guess. Commented Jul 27, 2021 at 14:18
  • @MatteoNNZ passwordProvider bean is created by providing configuration in properties file. For test classes, i dont think I should do that. I just wanted to execute the test cases without needing to create dataSource. Commented Jul 27, 2021 at 14:24
  • In this case you'll need a test profile, which builds the configuration in a different way than production (for example not creating the beans, or creating mock beans). Check more about Spring profiles Commented Jul 27, 2021 at 14:30
  • It appears you're re-declaring built-in configuration. Just let DataSourceAutoConfiguration do its job. Commented Jul 27, 2021 at 14:34

1 Answer 1

1

just mention in the spring boot test and it will load yours configuration class:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DBConfig.class})
public class SpringBootIntegrationTest {
    // ...
}
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.