I have a configuration class like the following
@Configuration
public class Configuration {
@Autowired
private JdbcTemplate jdbcTemplate;
@Bean
SimpleJdbcCall simpleJdbcCall() {
return new SimpleJdbcCall(jdbcTemplate).withProcedureName("");
}
}
I am trying to write a unit test for this configuration class. My test class looks like the following.
@ContextConfiguration(classes = { Configuration.class })
@RunWith(SpringRunner.class)
public class ConfigurationTest {
ApplicationContextRunner context = new ApplicationContextRunner()
.withUserConfiguration(Configuration.class);
@Test
public void should_check_presence_of_example_service() {
context.run(it -> {
assertThat(it).hasSingleBean(SimpleJdbcCall.class);
});
}
}
When I am running the test in the ConfigurationTest class, I am getting an error like the one below.
Error creating bean with name 'Configuration': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.jdbc.core.JdbcTemplate' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
I tried to solve this by crating a bean jdbcTemplate in the Configuration class and passed datasource. Then the test unit test does not find the bean datasource. After that I used @TestConfiguration in the ConfigurationTest class and crated a mock(jdbcTemplate). That did not work either.