gpt4 book ai didi

spring-batch - 无法在 Spring-Batch 中 Autowiring JobLauncherTestUtils

转载 作者:行者123 更新时间:2023-12-04 04:54:17 25 4
gpt4 key购买 nike

在对 spring-batch 中的一个步骤执行功能测试时,我遇到了以下错误。
得到以下错误:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No matching bean of type [org.springframework.batch.test.JobLauncherTestUtils] 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.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:924)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:793)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:707)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:478)

下面是用于此的配置文件和测试文件。

custom-context.xml 文件:
<batch:job id="custom.entities">
<batch:step id="entity.processor">
<batch:tasklet>
<batch:chunk reader="customReader" writer="customWriter" commit-interval="1" />
</batch:tasklet>
</batch:step>
</batch:job>

<bean id="customReader" class="com.batch.custom.EntityReader" scope="step">
<property name="providerId" value="#{jobParameters['providerId']}" />
</bean>

<bean id="customWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
<property name="resource" value="file:c:/Temp/ledgers-output.txt"/>
<property name="lineAggregator">
<bean class="org.springframework.batch.item.file.transform.PassThroughLineAggregator" />
</property>
</bean>

CustomJobTest.java 文件
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;

@Autowired
private ItemReader<WatchlistDataSet> reader;

@Test
@DirtiesContext
public void testLaunchJob() throws Exception {

JobParameters jobParameters = new JobParametersBuilder().addString("providerId", "cnp_1").toJobParameters();

JobExecution exec = jobLauncherTestUtils.launchStep("entity.processor", jobParameters);

assertEquals(BatchStatus.COMPLETED, exec.getStatus());

}

public JobLauncherTestUtils getJobLauncherTestUtils() {
return jobLauncherTestUtils;
}

public void setJobLauncherTestUtils(JobLauncherTestUtils jobLauncherTestUtils) {
this.jobLauncherTestUtils = jobLauncherTestUtils;
}

最佳答案

在谷歌搜索后发现需要在用于 JUnit 测试的 context.xml 中指定 bean 定义。

<bean id="jobLauncherTestUtils" class="org.springframework.batch.test.JobLauncherTestUtils" >
<property name="job" ref="custom.entities"/>
<property name="jobRepository" ref="jobRepository"/>
<property name="jobLauncher" ref="jobLauncher"/>
</bean>

根据上面的定义,我可以 Autowiring JobLauncherTestUtils。

关于spring-batch - 无法在 Spring-Batch 中 Autowiring JobLauncherTestUtils,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17379448/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com