如何为 Spring 批处理配置 class 注入测试值?
How can I inject a testing value for a Spring Batch configuration class?
我有一个 Spring 启动/批处理应用程序,想编写几个集成测试。该批处理有一个 FlatFileItemReader 并通过 yml 配置文件拉入要读取的文件。这是批处理配置 class:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Value("${file}")
private File file;
@Bean
public ItemReader<MyClass> reader(LineMapper<MyClass> lineMapper) {
FlatFileItemReader<MyClass> flatFileItemReader = new FlatFileItemReader<MyClass>();
flatFileItemReader.setResource(new FileSystemResource(file));
final int NUMBER_OF_HEADER_LINES = 1;
flatFileItemReader.setLinesToSkip(NUMBER_OF_HEADER_LINES);
flatFileItemReader.setLineMapper(lineMapper);
return flatFileItemReader;
}
用于测试 reader 的集成测试 class 是:
@SpringApplicationConfiguration(classes = LoadApplication.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
@Autowired
private ItemReader<MyClass> reader;
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@Bean
public StepExecution getStepExection() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
return execution;
}
@Test
public void testGoodData() throws Exception {
try {
File testFile = testFolder.newFile();
PrintWriter writer = new PrintWriter(testFile, "UTF-8");
writer.println("a,b,c");
writer.println("1,2,3");
writer.close();
//ReflectionTestUtils.setField(someObject, "file", testFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
assertNotNull(reader.read());
}
}
上面的测试代码中,someObject应该设置成什么?或者有其他方法可以注入测试文件吗?
在调用 testGoodData 之前已经创建了 reader。只需覆盖在 reader.
上设置的资源
即替换
ReflectionTestUtils.setField(someObject, "file", testFile);
用这行代码
reader.setResource(new FileSystemResource(testFile));
我有一个 Spring 启动/批处理应用程序,想编写几个集成测试。该批处理有一个 FlatFileItemReader 并通过 yml 配置文件拉入要读取的文件。这是批处理配置 class:
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Value("${file}")
private File file;
@Bean
public ItemReader<MyClass> reader(LineMapper<MyClass> lineMapper) {
FlatFileItemReader<MyClass> flatFileItemReader = new FlatFileItemReader<MyClass>();
flatFileItemReader.setResource(new FileSystemResource(file));
final int NUMBER_OF_HEADER_LINES = 1;
flatFileItemReader.setLinesToSkip(NUMBER_OF_HEADER_LINES);
flatFileItemReader.setLineMapper(lineMapper);
return flatFileItemReader;
}
用于测试 reader 的集成测试 class 是:
@SpringApplicationConfiguration(classes = LoadApplication.class)
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
@RunWith(SpringJUnit4ClassRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
@Autowired
private ItemReader<MyClass> reader;
@Rule
public TemporaryFolder testFolder = new TemporaryFolder();
@Bean
public StepExecution getStepExection() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
return execution;
}
@Test
public void testGoodData() throws Exception {
try {
File testFile = testFolder.newFile();
PrintWriter writer = new PrintWriter(testFile, "UTF-8");
writer.println("a,b,c");
writer.println("1,2,3");
writer.close();
//ReflectionTestUtils.setField(someObject, "file", testFile);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
assertNotNull(reader.read());
}
}
上面的测试代码中,someObject应该设置成什么?或者有其他方法可以注入测试文件吗?
在调用 testGoodData 之前已经创建了 reader。只需覆盖在 reader.
上设置的资源即替换
ReflectionTestUtils.setField(someObject, "file", testFile);
用这行代码
reader.setResource(new FileSystemResource(testFile));