我想找到一种单元测试自定义ItemReader的方法,我将其作为MultiResourceItemReader的委托编写。
这是我的Spring Batch XML配置文件:
<batch:job id="allegati" incrementer="jobParametersIncrementer">
<batch:step id="allegati-import">
<batch:tasklet>
<batch:chunk reader="allegati-reader" writer="allegati-writer" commit-interval="1"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="allegati-reader" class="org.springframework.batch.item.file.MultiResourceItemReader" scope="step">
<property name="resources" value="file:#{jobParameters['FILEPATH']}/*" />
<property name="delegate" ref="allegati-filereader" />
</bean>
<bean id="allegati-writer" class="org.springframework.batch.item.database.JpaItemWriter">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<bean id="allegati-filereader" class="it.infogroup.vertenze.porting.reader.AllegatiReader" />
以下是我尝试过的测试(我要测试的课程是AllegatiReader):
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:jobAllegati.xml"})
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
public class AllegatiTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Autowired
private AllegatiReader reader;
@PersistenceContext
protected EntityManager em;
public static final String PARAM_ID = "RUN ID";
public static final String PARAM_FILEPATH = "FILEPATH";
public static final String PARAM_FILEPATH_VALUE = "E:/Test/Vertenze/Porting/Allegati";
public StepExecution getStepExecution() {
JobParametersBuilder jpb = new JobParametersBuilder();
jpb.addString(PARAM_FILEPATH, PARAM_FILEPATH_VALUE);
jpb.addLong(PARAM_ID, System.currentTimeMillis());
StepExecution execution = MetaDataInstanceFactory.createStepExecution(jpb.toJobParameters());
return execution;
}
@Test
public void testReader() throws Exception {
Allegato allegato = reader.read();
Assert.assertNotNull(allegato);;
}
我的问题是资源(即FILEPATH中存在的文件)没有注入我的阅读器。
答案 0 :(得分:0)
我猜您正在尝试测试委托读者(Class AllegatiReader)?如果是这种情况,如果你想使用自动装配,你需要使用multiresourceitemreader作为测试中的bean