簡體   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