RESOLVED - issue BATCH-1481: Support injection of step-scoped dependencies into unit tests
Added StepScopeTestUtils. Revised a few test cases.
This commit is contained in:
@@ -10,12 +10,14 @@ import org.junit.Test;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.sample.domain.trade.internal.CustomerCreditIncreaseProcessor;
|
||||
import org.springframework.batch.test.JobLauncherTestUtils;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.batch.test.StepScopeTestExecutionListener;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
@@ -29,8 +31,9 @@ import org.springframework.test.context.support.DependencyInjectionTestExecution
|
||||
*
|
||||
* @author Robert Kasanicky
|
||||
*/
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml", "/jobs/ioSampleJob.xml" })
|
||||
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
|
||||
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml", "/job-runner-context.xml",
|
||||
"/jobs/ioSampleJob.xml" })
|
||||
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class })
|
||||
public abstract class AbstractIoSampleTests {
|
||||
|
||||
@Autowired
|
||||
@@ -111,4 +114,14 @@ public abstract class AbstractIoSampleTests {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a {@link StepExecution} that can be used to satisfy step scoped
|
||||
* dependencies in the test itself (not in the job it launches).
|
||||
*
|
||||
* @return a {@link StepExecution}
|
||||
*/
|
||||
protected StepExecution getStepExecution() {
|
||||
return MetaDataInstanceFactory.createStepExecution(getUniqueJobParameters());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,37 +16,40 @@
|
||||
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Dave Syer
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(locations = "/jobs/iosample/delimited.xml")
|
||||
public class DelimitedFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
private Map<String, String> jobParameters = Collections.singletonMap("fileName",
|
||||
"file:./target/test-outputs/delimitedOutput.csv");
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("fileName",
|
||||
"data/iosample/input/delimited.csv").toJobParameters();
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"data/iosample/input/delimited.csv").addString("outputFile",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -17,11 +17,13 @@
|
||||
package org.springframework.batch.sample.iosample;
|
||||
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -29,12 +31,20 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
@ContextConfiguration(locations = "/jobs/iosample/fixedLength.xml")
|
||||
public class FixedLengthFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
FlatFileItemReader<?> fileReader = (FlatFileItemReader<?>) reader;
|
||||
fileReader.setResource(outputResource);
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"file:./target/test-outputs/fixedLengthOutput.txt").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addString("inputFile",
|
||||
"data/iosample/input/fixedLength.txt").addString("outputFile",
|
||||
"file:./target/test-outputs/fixedLengthOutput.txt").toJobParameters();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -19,13 +19,17 @@ package org.springframework.batch.sample.iosample;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.scope.context.StepSynchronizationManager;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
/**
|
||||
* @author Dan Garrette
|
||||
* @author Dave Syer
|
||||
* @since 2.0
|
||||
*/
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@@ -34,9 +38,13 @@ public class JdbcPagingFunctionalTests extends AbstractIoSampleTests {
|
||||
|
||||
@Override
|
||||
protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) {
|
||||
// no-op
|
||||
JobParameters jobParameters = new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 0.)
|
||||
.toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
StepSynchronizationManager.close();
|
||||
StepSynchronizationManager.register(stepExecution);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected JobParameters getUniqueJobParameters() {
|
||||
return new JobParametersBuilder(super.getUniqueJobParameters()).addDouble("credit", 10000.).toJobParameters();
|
||||
|
||||
@@ -19,20 +19,25 @@ package org.springframework.batch.sample.iosample;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.Callable;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.StepExecution;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.file.FlatFileItemReader;
|
||||
import org.springframework.batch.item.ItemReader;
|
||||
import org.springframework.batch.item.ItemStream;
|
||||
import org.springframework.batch.sample.domain.trade.CustomerCredit;
|
||||
import org.springframework.batch.test.MetaDataInstanceFactory;
|
||||
import org.springframework.batch.test.StepScopeTestUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
@@ -49,13 +54,14 @@ public class TwoJobInstancesDelimitedFunctionalTests {
|
||||
private JobLauncher launcher;
|
||||
|
||||
@Autowired
|
||||
private AbstractJob job;
|
||||
private Job job;
|
||||
|
||||
@Autowired
|
||||
private FlatFileItemReader<CustomerCredit> reader;
|
||||
private ItemReader<CustomerCredit> reader;
|
||||
|
||||
@Autowired
|
||||
private Resource outputResource;
|
||||
@Qualifier("itemReader")
|
||||
private ItemStream readerStream;
|
||||
|
||||
@Test
|
||||
public void testLaunchJobTwice() throws Exception {
|
||||
@@ -69,18 +75,31 @@ public class TwoJobInstancesDelimitedFunctionalTests {
|
||||
|
||||
private void verifyOutput(int expected) throws Exception {
|
||||
|
||||
reader.setResource(outputResource);
|
||||
reader.open(new ExecutionContext());
|
||||
JobParameters jobParameters = new JobParametersBuilder().addString("fileName",
|
||||
"file:./target/test-outputs/delimitedOutput.csv").toJobParameters();
|
||||
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution(jobParameters);
|
||||
|
||||
int count = StepScopeTestUtils.doInStepScope(stepExecution, new Callable<Integer>() {
|
||||
|
||||
public Integer call() throws Exception {
|
||||
|
||||
int count = 0;
|
||||
|
||||
readerStream.open(new ExecutionContext());
|
||||
|
||||
try {
|
||||
while (reader.read() != null) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
readerStream.close();
|
||||
}
|
||||
return count;
|
||||
|
||||
int count = 0;
|
||||
try {
|
||||
while (reader.read() != null) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
finally {
|
||||
reader.close();
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
assertEquals(expected, count);
|
||||
|
||||
|
||||
@@ -26,10 +26,10 @@ import javax.sql.DataSource;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.batch.core.BatchStatus;
|
||||
import org.springframework.batch.core.Job;
|
||||
import org.springframework.batch.core.JobExecution;
|
||||
import org.springframework.batch.core.JobParameters;
|
||||
import org.springframework.batch.core.JobParametersBuilder;
|
||||
import org.springframework.batch.core.job.AbstractJob;
|
||||
import org.springframework.batch.core.launch.JobLauncher;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
|
||||
@@ -49,7 +49,7 @@ public class TwoJobInstancesPagingFunctionalTests {
|
||||
private JobLauncher launcher;
|
||||
|
||||
@Autowired
|
||||
private AbstractJob job;
|
||||
private Job job;
|
||||
|
||||
private SimpleJdbcTemplate jdbcTemplate;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user