gpt4 book ai didi

spring - JobLauncherTestUtils 在尝试测试 spring 批处理步骤时抛出 NoUniqueBeanDefinitionException

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

我正在使用 Spring boot 和 Spring batch。我已经定义了不止一项工作。

我正在尝试构建 junit 来测试作业中的特定任务。

因此,我正在使用 JobLauncherTestUtils 库。

当我运行我的测试用例时,我总是得到 NoUniqueBeanDefinitionException。

这是我的测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {BatchConfiguration.class})
public class ProcessFileJobTest {

@Configuration
@EnableBatchProcessing
static class TestConfig {
@Autowired

private JobBuilderFactory jobBuilder;
@Autowired
private StepBuilderFactory stepBuilder;

@Bean
public JobLauncherTestUtils jobLauncherTestUtils() {
JobLauncherTestUtils jobLauncherTestUtils = new JobLauncherTestUtils();
jobLauncherTestUtils.setJob(jobUnderTest());
return jobLauncherTestUtils;
}


@Bean
public Job jobUnderTest() {
return jobBuilder.get("job-under-test")
.start(processIdFileStep())
.build();
}


@Bean
public Step processIdFileStep() {
return stepBuilder.get("processIdFileStep")
.<PushItemDTO, PushItemDTO>chunk(1) //important to be one in this case to commit after every line read
.reader(reader(null))

.processor(processor(null, null, null, null))
.writer(writer())

// .faultTolerant()
// .skipLimit(10) //default is set to 0
// .skip(MySQLIntegrityConstraintViolationException.class)
.build();
}


@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemStreamReader<PushItemDTO> reader(@Value("#{jobExecutionContext[filePath]}") String filePath) {
...
return itemReader;
}

@Bean
@Scope(value = "step", proxyMode = ScopedProxyMode.INTERFACES)
public ItemProcessor<PushItemDTO, PushItemDTO> processor(@Value("#{jobParameters[pushMessage]}") String pushMessage,
@Value("#{jobParameters[jobId]}") String jobId,
@Value("#{jobParameters[taskId]}") String taskId,
@Value("#{jobParameters[refId]}") String refId)
{
return new PushItemProcessor(pushMessage,jobId,taskId,refId);
}

@Bean
public LineMapper<PushItemDTO> lineMapper() {
DefaultLineMapper<PushItemDTO> lineMapper = new DefaultLineMapper<PushItemDTO>();
...
return lineMapper;
}

@Bean
public ItemWriter writer() {
return new someWriter();
}
}


@Autowired
protected JobLauncher jobLauncher;

@Autowired
JobLauncherTestUtils jobLauncherTestUtils;



@Test
public void processIdFileStepTest1() throws Exception {
JobParameters jobParameters = new JobParametersBuilder().addString("filePath", "C:\\etc\\files\\2015_02_02").toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchStep("processIdFileStep",jobParameters);

}

那是异常(exception):

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [org.springframework.batch.core.Job] is defined: expected single matching bean but found 3: jobUnderTest,executeToolJob,processFileJob

有什么想法吗?谢谢。

添加了 BatchConfiguration 类:

package com.mycompany.notification_processor_service.batch.config;

import com.mycompany.notification_processor_service.common.config.CommonConfiguration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.*;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;

import javax.sql.DataSource;


@ComponentScan("com.mycompany.notification_processor_service.batch")
@PropertySource("classpath:application.properties")
@Configuration
@Import({CommonConfiguration.class})
@ImportResource({"classpath:applicationContext-pushExecuterService.xml"/*,"classpath:si/integration-context.xml"*/})
public class BatchConfiguration {

@Value("${database.driver}")
private String databaseDriver;
@Value("${database.url}")
private String databaseUrl;
@Value("${database.username}")
private String databaseUsername;
@Value("${database.password}")
private String databasePassword;



@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName(databaseDriver);
dataSource.setUrl(databaseUrl);
dataSource.setUsername(databaseUsername);
dataSource.setPassword(databasePassword);
return dataSource;
}

@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

}

这是CommonConfiguration

@ComponentScan("com.mycompany.notification_processor_service")
@Configuration
@EnableJpaRepositories(basePackages = {"com.mycompany.notification_processor_service.common.repository.jpa"})
@EnableCouchbaseRepositories(basePackages = {"com.mycompany.notification_processor_service.common.repository.couchbase"})
@EntityScan({"com.mycompany.notification_processor_service"})
@EnableAutoConfiguration
@EnableTransactionManagement
@EnableAsync
public class CommonConfiguration {

}

最佳答案

我有同样的问题,更简单的方法是注入(inject) JobLauncherTestUtils 的 setter,就像 Mariusz 在 Jira of Spring 中解释的那样:

@Bean
public JobLauncherTestUtils getJobLauncherTestUtils() {

return new JobLauncherTestUtils() {
@Override
@Autowired
public void setJob(@Qualifier("ncsvImportJob") Job job) {
super.setJob(job);
}
};
}

关于spring - JobLauncherTestUtils 在尝试测试 spring 批处理步骤时抛出 NoUniqueBeanDefinitionException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28293266/

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