Spring 批处理:对自定义项目进行单元测试 reader
Spring Batch: Unit testing a custom item reader
我想找到一种方法来对我作为 MultiResourceItemReader 的委托编写的自定义 ItemReader 进行单元测试。
这是我的 Spring 批处理 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" />
这是我尝试进行的测试(class 我想测试的是 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 中的文件)没有注入我的 reader。
我猜您正在尝试测试代理 reader(Class AllegatiReader)?如果是这样,并且如果你想使用自动装配,你需要使用 multiresourceitemreader 作为 bean under test
原因
- multiresourceitemreader 创建完整的委托 reader
对象(完整的资源集)
- (delegate) reader bean 本身不会(不能)作为一个完整的
使用资源
配置 reader
我想找到一种方法来对我作为 MultiResourceItemReader 的委托编写的自定义 ItemReader 进行单元测试。
这是我的 Spring 批处理 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" />
这是我尝试进行的测试(class 我想测试的是 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 中的文件)没有注入我的 reader。
我猜您正在尝试测试代理 reader(Class AllegatiReader)?如果是这样,并且如果你想使用自动装配,你需要使用 multiresourceitemreader 作为 bean under test
原因
- multiresourceitemreader 创建完整的委托 reader 对象(完整的资源集)
- (delegate) reader bean 本身不会(不能)作为一个完整的 使用资源 配置 reader