繁体   English   中英

Spring Batch:对自定义项目阅读器进行单元测试

[英]Spring Batch: Unit testing a custom item reader

我想找到一种方法来对作为MultiResourceItemReader的委托编写的自定义ItemReader进行单元测试。

这是我的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中存在的文件)没有注入到我的阅读器中。

我猜您正在尝试测试委托阅读器(Class AllegatiReader)? 如果是这样,并且如果要使用自动装配,则需要将multiresourceitemreader用作被测bean

原因

  • multiresourceitemreader创建完整的委托阅读器对象(带有资源集)
  • 不会(不能)将(代理)读取器bean本身作为具有资源的完全配置的读取器提供

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM