'spring batch+spring boot+java config+test cases
spring batch+spring boot+java config+test cases
I have followed the below example and my use case matches this, I have implemented the project with similar setup everything works fine.
I am struck with writing test case , could someone s=throw some light or show me the approach for writing he unit test cases..
spring batch+spring boot+java config+test cases
//@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = {JobConfiguration.class,})
@ComponentScan("uk.gov.iebr.batch.processor")
@SpringApplicationConfiguration(classes = IEBRRecommendInterventionsApplication.class)
@RunWith(SpringRunner.class)
@Import(IntegrationTestConfiguration.class)
@SpringBootTest
public class BatchJobRecommendTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
public DataSource dataSource;
@Test
public void testJob() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
Assert.assertEquals(1, jobExecution.getStepExecutions().size());
}
@Test
public void testStep() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
Assert.assertEquals(ExitStatus.COMPLETED, jobExecution.getExitStatus());
}
I wrote this test case but , getting Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jobConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public javax.sql.DataSource uk.gov.iebr.batch.configuration.JobConfiguration.dataSource; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.sql.DataSource] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.
PLease show some directiosn or links where i can draw reference or github code base where something similair test cases are implemented ...
Solution 1:[1]
I wrote this test case but , getting Caused by: org.springframework.beans.factory.BeanCreationException:
BeanCreationException came when there is a problem in JPA Query or an error in class.
AutowiredAnnotationBeanPostProcessor
In your case, it shows some errors in Autowired. Check you Query and Class that Annotated with @Component, @Service.
Solution 2:[2]
This is up and running for me:
import com.mastercard.installments.offers.processor.common.JobConstants;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.*;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
@SpringBatchTest
@RunWith(SpringRunner.class)
@ActiveProfiles(profiles = "test")
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class JobLauncherCIT {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testJobLauncher() throws Exception {
JobExecution jobExecution = jobLauncherTestUtils.launchJob(new JobParameters(JobHelper.getJobParameters()));
Collection<StepExecution> stepExecutions = jobExecution.getStepExecutions();
List<StepExecution> arrStepExecution = new ArrayList<>(stepExecutions);
StepExecution stepExecution = arrStepExecution.get(1);
if (stepExecution != null) {
assertEquals(3, stepExecution.getReadCount());
assertEquals(3, stepExecution.getWriteCount());
}
JobInstance jobInstance = jobExecution.getJobInstance();
assertEquals("fetchConsumerInfoJob", jobInstance.getJobName());
ExitStatus jobExitStatus = jobExecution.getExitStatus();
assertEquals(JobConstants.COMPLETED, jobExitStatus.getExitCode());
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Navin Kumar |
Solution 2 | KPK |