I'm trying to test a job but I can't get to the bottom of it, basically my step consists of a single reader and a composite Processor and a Composite writer, all this because I have to read from a single .CSV and write on MongoDB and Mysql, here is my Job Configuration class:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
public MarketDataMySqlRepository marketDataMySqlRepository;
@Value("${file.input}")
private String fileInput;
@Bean
public TaskExecutor taskExecutor(){
SimpleAsyncTaskExecutor taskExecutor=new SimpleAsyncTaskExecutor("customerInfoThreads-");
return taskExecutor;
}
@Bean
public FlatFileItemReader<MarketDataDto> reader() {
return new FlatFileItemReaderBuilder<MarketDataDto>().name("ItemReader")
.resource(new PathResource(fileInput)).delimited()
.names("time","id", "price", "quantity", "isbuyermaker")
.fieldSetMapper(new BeanWrapperFieldSetMapper<MarketDataDto>() {
{
setTargetType(MarketDataDto.class);
}
}).build();
}
@Bean
public MongoItemWriter<MarketDataMongo> writer(MongoTemplate mongoTemplate) {
return new MongoItemWriterBuilder<MarketDataMongo>().template(mongoTemplate).collection("marketdata")
.build();
}
@Bean
public RepositoryItemWriter<MarketDataMySql> writer1() {
RepositoryItemWriter<MarketDataMySql> writer = new RepositoryItemWriter<>();
writer.setRepository(marketDataMySqlRepository);
writer.setMethodName("save");
return writer;
}
@Bean
public CompositeItemWriter CompositeItemWriter(MongoTemplate mongoTemplate){
CompositeItemWriter writer = new CompositeItemWriter();
writer.setDelegates(Arrays.asList(writer(mongoTemplate),writer1()));
return writer;
}
@Bean
public UserItemProcessorMongo MongoProcessor() {
return new UserItemProcessorMongo();
}
@Bean
public UserItemProcessorMySql MySqlProcessor() {return new UserItemProcessorMySql();}
@Bean
public CompositeItemProcessor compositeProcessor() throws Exception {
List<ItemProcessor<MarketDataDto,MarketDataMongo>> itemProcessors = new ArrayList<>(1);
List<ItemProcessor<MarketDataDto,MarketDataMySql>> itemProcessors1 = new ArrayList<>(1);
itemProcessors1.add(MySqlProcessor());
itemProcessors.add(MongoProcessor());
CompositeItemProcessor processor = new CompositeItemProcessor();
processor.setDelegates(itemProcessors);
processor.setDelegates(itemProcessors1);
//processor.afterPropertiesSet();
return processor;
}
@Bean
public Step step1(FlatFileItemReader<MarketDataDto> itemReader, MongoItemWriter<MarketDataMongo> itemWriter, MongoTemplate mongoTemplate)
throws Exception {
return this.stepBuilderFactory.get("step1").chunk(200000).reader(itemReader)
.processor(compositeProcessor()).writer(CompositeItemWriter(mongoTemplate)).build();
}
@Bean
public Job updateUserJob(JobCompletionNotificationListener listener, Step step1)
throws Exception {
return this.jobBuilderFactory.get("updateMarketJob").incrementer(new RunIdIncrementer())
.listener(listener).start(step1).build();
}
}
And this is my TestClass :
@RunWith(SpringRunner.class)
@SpringBatchTest
@ContextConfiguration(classes= {BatchConfiguration.class, BatchAutoConfiguration.class})
public class BatchConfigurationTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private JobRepositoryTestUtils jobRepositoryTestUtils;
@Autowired
private MarketDataMySqlRepository marketDataMySqlRepository;
@Test
public void readerTest() throws Exception {
JobParameters jobParameters = this.jobLauncherTestUtils.getUniqueJobParameters();
JobExecution jobExecution = this.jobLauncherTestUtils.launchJob(jobParameters);
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
}
When i try to launch my test i riceive this Exception :
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:124)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:118)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:248)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329)
at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306)
at org.junit.runners.ParentRunner.run(ParentRunner.java:413)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38)
at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11)
at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35)
at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:235)
at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:54)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jobRepositoryTestUtils': Unsatisfied dependency expressed through method 'setDataSource' parameter 0; nested exception is 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:767)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:719)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:734)
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:308)
at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:132)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:141)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:90)
... 27 more
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:1801)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1357)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1311)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.resolveMethodArguments(AutowiredAnnotationBeanPostProcessor.java:759)
... 46 more
CodePudding user response:
Spring Batch automatically creates tables in db, do you have a database launched and linked to your project ?
If not, use the application.properties file to bind your project to the database, Exemple for Postgres database, with db name dbName :
spring.datasource.platform=postgres
spring.datasource.jdbc-url=jdbc:postgresql://host:port/dbName
spring.datasource.username=postgres
spring.datasource.password=postgres
CodePudding user response:
I answer to myself, maybe it can be useful to someone, I created a configuration class for my Datasource with a configuration bean inside:
public class DataSourceConfig {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("org.h2.Driver");
dataSource.setUrl("jdbc:h2:mem:db;DB_CLOSE_DELAY=-1");
dataSource.setUsername("sa");
dataSource.setPassword("sa");
return dataSource;
}}
although i solved this problem now i have others lol, i hope it will be useful to someone :-)