Reputation:
I'm doing a PoC for a Spring Batch application. So far everything works fine, but I can't get the test(s) to pick up the Datasource
, it always fails with:
...
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1493)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1104)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1066)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:835)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 42 more
This is my application.yml
file:
server:
port: ${port:9080}
spring:
http:
encoding:
charset: UTF-8
enabled: true
force: true
output:
ansi:
enabled: detect
profiles: # default, development, production
active: default, development
---
spring:
datasource:
driver-class-name: org.h2.Driver
sql-script-encoding: UTF-8
url: jdbc:h2:mem:JavaSpringBootBatch;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;MODE=MySQL
username: sa
password:
h2:
console:
enabled: true
path: /h2-console
profiles: development
---
server:
port: ${port:80}
spring:
datasource:
initialize: false
# ...
profiles: production
...and my test class(es):
@Configuration
@Import(AppConfig.class) // This eventually "imports" the app's config
public class TestConfig {
@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
return new JobLauncherTestUtils();
}
}
@ActiveProfiles("development")
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = TestConfig.class)
//@TestExecutionListeners({ DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
public class PlayerItemBatchTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void readProcessWriteBatchProcess() throws Exception {
final JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assertions.assertThat(jobLauncherTestUtils.launchJob().getStatus()).isEqualTo(BatchStatus.COMPLETED);
}
}
@Configuration
public class BatchConfig { // Included with AppConfig
private final DataSource dataSource;
@Autowired
public BatchConfig(final DataSource dataSource) {
this.dataSource = dataSource;
}
// ...
}
When I start my application I can clearly see that datasource
has everything (all settings) I have in my development
profile from the YAML. When I try to execute the test case(s) it just fails stating "it can't find the datasource".
Upvotes: 0
Views: 1484
Reputation: 96
Usually I do an explicit configuration of org.springframework.batch.core.configuration.annotation.BatchConfigurer when using Spring Batch in conjunction with Spring Boot. This gives better control - you could find an example here: Spring Batch with Spring Boot Integration testing
Upvotes: 0
Reputation: 5703
it seems to be a dependency configuration problem. You need a dependency on spring-jdbc for an embedded database to be auto-configured.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
Here is doc it can help you.
Upvotes: 0
Reputation: 100
Apart from using @EnableConfigurationProperties Try to create a .yml file in test/resources and create a TestConfig.java in test/src with @TestConfiguration annotations .
Upvotes: 0