2010-11-02 36 views
5

Karmaşık bir yığın uygulamasına sahibim ve akış hakkındaki varsayımların doğru olduğunu test etmek istiyorum.Spring-Batch'de iş akışını test etmenin en iyi yolu nedir?

İşte Birlikte çalıştığım şeyi çok daha basitleştirilmiş versiyonu:

<beans> 
    <batch:job id="job1"> 
    <batch:step id="step1" next="step2"> 
     <batch:tasklet ref="someTask1"/> 
    </batch:step> 
    <batch:step id="step2.master"> 
     <batch:partition partitioner="step2Partitioner" 
      step="step2" /> 
     <batch:next on="*" to="step3" /> 
     <batch:next on="FAILED" to="step4" /> 
    </batch:step> 
    <batch:step id="step3" next="step3"> 
     <batch:tasklet ref="someTask1"/> 
    </batch:step> 
    <batch:step id="step4" next="step4"> 
     <batch:tasklet ref="someTask1"/> 
    </batch:step> 
    </batch:job> 
    <batch:job id="job2"> 
    <batch:step id="failingStep"> 
     <batch:tasklet ref="failingTasklet"/> 
    </batch:step> 
    </batch:job> 

    <bean id="step2Partitioner" class="org.springframework.batch.core.partition.support.MultiResourcePartitioner" scope="step"> 
    <property name="resources" value="file:${file.test.resources}/*" /> 
    </bean> 

    <bean id="step2" class="org.springframework.batch.core.step.job.JobStep"> 
    <property name="job" ref="job2" /> 
    <property name="jobLauncher" ref="jobLauncher" /> 
    <property name="jobRepository" ref="jobRepository" /> 
    </bean> 
</beans> 

Job1 ben test etmek istiyorum iştir. Gerçekten sadece step2.master'ın adım 3 veya adım 4'e geçişini test etmek istiyorum. Ben

Ancak, bu test, altta yatan eylemleri yapılandırmayı değil test edildiğinden, bozulmamış Job1 teknik verilerine devam etmek istiyorum ... hiç Step1 test etmek istemiyoruz. Uçtan uca şeyler test etmek için zaten kabul testlerim var. Bu örnekte, her bir kenar durumu için ayrı uçtan uca testler oluşturmadan küçük varyasyonlar için hedeflenmiş testler yazabilirim.

step2 iç işi başarısız olduğunda, step2.master 4. adıma ve bu test etmek için iyi bir yolu var mı 3. adıma bunu yapmamamı üzerinde iletecek Ne ben test etmek istiyorum nedir?

cevap

8

Her zaman başarısız ve step3 ve step4 çağrıldı olup olmadığını kontrol etmek için bir StepExecutionListener kullanmak sahte uygulaması ile adım2 yerini alabilir.

burada iyi örnekler vardır: http://static.springsource.org/spring-batch/reference/html/testing.html#endToEndTesting

+0

Ama ne adım1 ben test etmek istemiyorum çok karmaşık bir işlemdir ve bu örnekle temsil edilmeyen diğer karmaşık işlemler varsa? Temel olarak, SADECE bölümlenmiş işteki bir hatanın beni doğru bir sonraki adıma göndereceğini ve devam eden diğer şeylerden herhangi birini test etmeyeceğini test etmek istiyorum. –

+2

Karmaşık işleri, bir sonraki iş için gerekenleri sağlayan sahte uygulamalarla değiştirebilirsiniz (herhangi bir bağımlılık varsa). Örneğin, 1. adımın bir db okuduğunu ve bir klasörde bir dosya oluşturduğunu söyleyin. Bunu, bir test dosyasını çıktı klasörüne taşıyan bir alayla değiştirin. – esmiralha

+0

Böylece temelde Evet, kolayca "bazı" yapılandırma çalıştığını ... Ben de kolayca benim özel yapılandırma çalıştığını doğrulamak doğrulayabilir ... başka yapılandırma çalışacak olmadığını doğrulamak için ikinci bir yapılandırması oluşturmak gerekir? –

2

ayrı ayrı her adımı test edebilirsiniz. Örnek: Ben yardımcı olur umarım

JobLauncherTestUtil jobLauncherTestUtil = new JobLauncherTestUtil(); 
jobLauncherTestUtil.setJobLauncher(jobLauncher); 
jobLauncherTestUtil.setJob(job); 
jobLauncherTestUtil.setJobRepository(jobRepository); 
Map<String, JobParameter> params = Maps.newHashMap(); 
//determine job params here: 
params.put(....); 
JobParameters jobParams = new JobParameters(params); 
ExecutionContext context = new ExecutionContext(); 
//put something to job context, if you need. 
context.put(...); 
JobExecution jobExecution = jobLauncherTestUtil.launchStep("stepId",jobParams,context); 

Assert.assertEquals("Step stepId failed", ExitStatus.COMPLETED, execution.getExitStatus()) 

.

İlgili konular