SyntaxHighlighter

Monday, July 15, 2013

Spring Batch In-Memory Configuration

Spring Batch In-Memory Configuration


here's a snippet to illustrate configuration of Spring Batch Infrastructure (jobRepository, jobExplorer, jobLauncher) purely in-memory for typical JUnit Integration testing.



 
  
 

 
  
 
 
 
  
  
 

 
 
 




it gives you the basics to support a simple unit test with the following setup;

package de.incompleteco.spring.batch.job;

import static org.junit.Assert.assertFalse;

import javax.annotation.Resource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes=SimpleJobConfig.class)
public class SimpleJobConfigTest {

 @Resource
 private Job job;
 
 @Resource
 private JobLauncher jobLauncher;
 
 @Resource
 private JobExplorer jobExplorer;
 
 @Test
 public void test() throws Exception  {
  //run the job
  JobExecution execution = jobLauncher.run(job,new JobParametersBuilder().toJobParameters());
  //monitor
  while (jobExplorer.getJobExecution(execution.getId()).isRunning()) {
   Thread.sleep(100);
  }//end while
  //load
  execution = jobExplorer.getJobExecution(execution.getId());
  //check
  assertFalse(execution.getStatus().isUnsuccessful());
 }


No comments:

Post a Comment