spring批处理+spring boot+java配置+测试用例
我遵循了下面的示例,我的用例与此匹配,我已经使用类似的设置实现了该项目,一切正常。
我对编写测试用例感到震惊,有人可以给我一些启发或向我展示编写单元测试用例的方法..
spring批处理+spring boot+java配置+测试用例
//@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.
请显示一些指导或链接,我可以在其中引用或实现类似测试用例的 github 代码库...
I wrote this test case but , getting Caused by: org.springframework.beans.factory.BeanCreationException:
当 JPA 查询出现问题或类中出现错误时,会出现 BeanCreationException。
AutowiredAnnotationBeanPostProcessor
在您的情况下,它显示 Autowired 中的一些错误。检查使用 @Component、@Service 注释的查询和类。
这对我来说已经启动并运行了:
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());
}
}
看来,您的数据源bean尚未在处于活动状态的应用程序上下文中创建。所以您需要检查的是
如果没有什么问题的话。数据源 bean 应该在应用程序上下文中呈现。
如果您需要示例源代码,请告诉我,我会毫无问题地安排它