1. Unit Testing
与其他应用程序样式一样,对作为批处理作业一部分编写的任何代码进行单元测试都非常重要。 Spring 核心文档详细介绍了如何对 Spring 进行单元测试和集成测试,因此在此不再赘述。但是,重要的是要考虑如何“端对端”测试批处理作业,这是本章要介绍的内容。 spring-batch-testItem 包含有助于这种端到端测试方法的类。
1.1.创建单元测试类
为了使单元测试运行批处理作业,框架必须加载作业的ApplicationContext
。使用两个 Comments 来触发此行为:
-
@RunWith(SpringRunner.class)
:指示该类应使用 Spring 的 JUnit 工具 -
@ContextConfiguration(…)
:指示用于配置ApplicationContext
的资源。
从 v4.1 开始,还可以使用@SpringBatchTest
注解在测试上下文中注入诸如JobLauncherTestUtils
和JobRepositoryTestUtils
之类的 Spring Batch 测试 Util。
以下示例显示了正在使用的 Comments:
使用 Java 配置
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SkipSampleConfiguration.class)
public class SkipSampleFunctionalTests { ... }
使用 XML 配置
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
"/jobs/skipSampleJob.xml" })
public class SkipSampleFunctionalTests { ... }
1.2.批处理作业的端到端测试
“端到端”测试可以定义为从头到尾测试批处理作业的完整运行。这样就可以进行测试,以设置测试条件,执行工作并验证最终结果。
在以下示例中,批处理作业从数据库读取并写入平面文件。测试方法首先使用测试数据构建数据库。它将清除 CUSTOMER 表,然后插入 10 条新记录。然后,测试使用launchJob()
方法启动Job
。 JobLauncherTestUtils
类提供了launchJob()
方法。 JobLauncherTestUtils
类还提供launchJob(JobParameters)
方法,该方法允许测试提供特定的参数。 launchJob()
方法返回JobExecution
对象,这对于声明有关Job
运行的特定信息很有用。在以下情况下,测试将验证Job
以状态“ COMPLETED”结束:
基于 XML 的配置
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(locations = { "/simple-job-launcher-context.xml",
"/jobs/skipSampleJob.xml" })
public class SkipSampleFunctionalTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Test
public void testJob() throws Exception {
simpleJdbcTemplate.update("delete from CUSTOMER");
for (int i = 1; i <= 10; i++) {
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
i, "customer" + i);
}
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
}
}
基于 Java 的配置
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes=SkipSampleConfiguration.class)
public class SkipSampleFunctionalTests {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private SimpleJdbcTemplate simpleJdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
}
@Test
public void testJob() throws Exception {
simpleJdbcTemplate.update("delete from CUSTOMER");
for (int i = 1; i <= 10; i++) {
simpleJdbcTemplate.update("insert into CUSTOMER values (?, 0, ?, 100000)",
i, "customer" + i);
}
JobExecution jobExecution = jobLauncherTestUtils.launchJob();
Assert.assertEquals("COMPLETED", jobExecution.getExitStatus().getExitCode());
}
}
1.3.测试单个步骤
对于复杂的批处理作业,端到端测试方法中的测试用例可能变得难以 Management。在这些情况下,使用测试用例自行测试各个步骤可能会更有用。 JobLauncherTestUtils
类包含一个名为launchStep
的方法,该方法带有一个步骤名并仅运行特定的Step
。这种方法允许进行更具针对性的测试,从而使测试仅针对该步骤设置数据并直接验证其结果。下面的示例显示如何使用launchStep
方法按名称加载Step
:
JobExecution jobExecution = jobLauncherTestUtils.launchStep("loadFileStep");
1.4.测试步骤范围的组件
通常,在运行时为您的步骤配置的组件会使用步骤作用域和后期绑定,以从步骤或作业执行中注入上下文。将它们作为独立的组件进行测试非常棘手,除非您有一种将上下文设置为好像它们在一步执行中的方式。这是 Spring Batch 中两个组件的目标:StepScopeTestExecutionListener
和StepScopeTestUtils
。
侦听器在类级别声明,其工作是为每个测试方法创建一个步骤执行上下文,如以下示例所示:
@ContextConfiguration
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
StepScopeTestExecutionListener.class })
@RunWith(SpringRunner.class)
public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless
// a step is active...
@Autowired
private ItemReader<String> reader;
public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("input.data", "foo,bar,spam");
return execution;
}
@Test
public void testReader() {
// The reader is initialized and bound to the input data
assertNotNull(reader.read());
}
}
有两个TestExecutionListeners
。一种是常规的 Spring Test 框架,该框架处理来自配置的应用程序上下文的依赖项注入以注入 Reader。另一个是 Spring Batch StepScopeTestExecutionListener
。它通过在StepExecution
的测试用例中查找工厂方法来工作,并使用该工厂方法作为测试方法的上下文,就好像该执行在Step
中在运行时处于活动状态一样。工厂方法通过其签名进行检测(必须返回StepExecution
)。如果未提供工厂方法,则会创建默认的StepExecution
。
从 v4.1 开始,如果使用@SpringBatchTest
Comments 测试类,则将StepScopeTestExecutionListener
和JobScopeTestExecutionListener
导入为测试执行侦听器。前面的测试示例可以配置如下:
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration
public class StepScopeTestExecutionListenerIntegrationTests {
// This component is defined step-scoped, so it cannot be injected unless
// a step is active...
@Autowired
private ItemReader<String> reader;
public StepExecution getStepExecution() {
StepExecution execution = MetaDataInstanceFactory.createStepExecution();
execution.getExecutionContext().putString("input.data", "foo,bar,spam");
return execution;
}
@Test
public void testReader() {
// The reader is initialized and bound to the input data
assertNotNull(reader.read());
}
}
如果您希望步骤作用域的持续时间是测试方法的执行,则侦听器方法很方便。对于更灵活但更具侵入性的方法,可以使用StepScopeTestUtils
。以下示例计算了上一示例中显示的 Reader 中可用 Item 的数量:
int count = StepScopeTestUtils.doInStepScope(stepExecution,
new Callable<Integer>() {
public Integer call() throws Exception {
int count = 0;
while (reader.read() != null) {
count++;
}
return count;
}
});
1.5.验证输出文件
当批处理作业写入数据库时,很容易查询数据库以验证输出是否符合预期。但是,如果批处理作业写入文件,则验证输出同样重要。 Spring Batch 提供了一个名为AssertFile
的类,以方便验证输出文件。名为assertFileEquals
的方法接受两个File
对象(或两个Resource
对象),并逐行 assert 两个文件具有相同的内容。因此,可以创建一个具有预期输出的文件,并将其与实际结果进行比较,如以下示例所示:
private static final String EXPECTED_FILE = "src/main/resources/data/input.txt";
private static final String OUTPUT_FILE = "target/test-outputs/output.txt";
AssertFile.assertFileEquals(new FileSystemResource(EXPECTED_FILE),
new FileSystemResource(OUTPUT_FILE));
1.6.模拟域对象
在编写 Spring Batch 组件的单元测试和集成测试时遇到的另一个常见问题是如何模拟域对象。很好的例子是StepExecutionListener
,如以下代码片段所示:
public class NoWorkFoundStepExecutionListener extends StepExecutionListenerSupport {
public ExitStatus afterStep(StepExecution stepExecution) {
if (stepExecution.getReadCount() == 0) {
return ExitStatus.FAILED;
}
return null;
}
}
框架提供了前面的侦听器示例,并检查StepExecution
的空读取计数,从而表示未完成任何工作。尽管这个示例非常简单,但是它用于说明尝试对实现需要 Spring Batch 域对象的接口的类进行单元测试时可能遇到的问题类型。在前面的示例中,为侦听器考虑以下单元测试:
private NoWorkFoundStepExecutionListener tested = new NoWorkFoundStepExecutionListener();
@Test
public void noWork() {
StepExecution stepExecution = new StepExecution("NoProcessingStep",
new JobExecution(new JobInstance(1L, new JobParameters(),
"NoProcessingJob")));
stepExecution.setExitStatus(ExitStatus.COMPLETED);
stepExecution.setReadCount(0);
ExitStatus exitStatus = tested.afterStep(stepExecution);
assertEquals(ExitStatus.FAILED.getExitCode(), exitStatus.getExitCode());
}
由于 Spring Batch 域模型遵循良好的面向对象原则,因此StepExecution
需要一个JobExecution
(需要JobInstance
和JobParameters
)来创建有效的StepExecution
。尽管这在固态域模型中很好,但确实会使创建用于单元测试的存根对象变得冗长。为了解决这个问题,Spring Batch 测试模块包括一个用于创建域对象的工厂:MetaDataInstanceFactory
。有了这个工厂,可以将单元测试更新为更简洁,如以下示例所示:
private NoWorkFoundStepExecutionListener tested = new NoWorkFoundStepExecutionListener();
@Test
public void testAfterStep() {
StepExecution stepExecution = MetaDataInstanceFactory.createStepExecution();
stepExecution.setExitStatus(ExitStatus.COMPLETED);
stepExecution.setReadCount(0);
ExitStatus exitStatus = tested.afterStep(stepExecution);
assertEquals(ExitStatus.FAILED.getExitCode(), exitStatus.getExitCode());
}
前面用于创建简单StepExecution
的方法只是工厂中提供的一种便捷方法。完整的方法列表可在其Javadoc中找到。