如何在不运行整个作业的情况下测试Spring Batch步骤



我正在开发一个Spring Batch应用程序,该应用程序在一个作业中有两个步骤。我试着单独测试每一步。根据Spring文档,我应该能够使用JobLauncherTestUitls.launchStep()我为的一个步骤设置了以下测试

@SpringBootTest
@SpringBatchTest
@EnableAutoConfiguration
@ContextConfiguration(classes = {JobConfig.class, EmailAndArchiveStepConfig.class, UpdateFactorReserveConfig.class})
@ExtendWith(SpringExtension.class)
@ActiveProfiles("test")
class ConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
private JdbcTemplate jdbcTemplate;
@Autowired
public void setDataSource(DataSource dataSource) {
this.jdbcTemplate = new JdbcTemplate(dataSource);
}
@Test
@DisplayName("Testing Email and Archive Configured")
public void testEmailAndArchiveConfig(){
JobExecution jobExecution = jobLauncherTestUtils.launchStep("Email and Archive", executionContext);
assertEquals(new ExitStatus("COMPLETED").getExitCode() ,jobExecution.getExitStatus().getExitCode());
}

然而,当我运行这个测试时,它会从头开始作业,同时运行另一个步骤,而不仅仅是运行我想要测试的这个步骤。我一直找不到任何解决办法。

您可能看到的是,当您运行测试时,Spring Boot默认运行您的作业。您可以通过将spring.batch.job.enabled=false添加到测试属性中来禁用它。

使用jobLauncherTestUtils.launchStep应该只启动步骤,而不是整个作业,这里有一个快速的自包含示例:

import javax.sql.DataSource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.batch.test.context.SpringBatchTest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;
@SpringBatchTest
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = ConfigTest.JobConfig.class)
public class ConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testStep1() {
JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");
assertEquals(new ExitStatus("COMPLETED").getExitCode() ,jobExecution.getExitStatus().getExitCode());
}
@Configuration
@EnableBatchProcessing
public static class JobConfig {
@Bean
public Step step1(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("step1")
.tasklet((contribution, chunkContext) -> {
System.out.println("hello");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Step step2(StepBuilderFactory stepBuilderFactory) {
return stepBuilderFactory.get("step2")
.tasklet((contribution, chunkContext) -> {
System.out.println("world");
return RepeatStatus.FINISHED;
})
.build();
}
@Bean
public Job job(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
return jobBuilderFactory.get("job")
.start(step1(stepBuilderFactory))
.next(step2(stepBuilderFactory))
.build();
}
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.setType(EmbeddedDatabaseType.HSQL)
.addScript("/org/springframework/batch/core/schema-hsqldb.sql")
.build();
}
}
}

此测试打印hello,这意味着它只启动了step1,而不是整个作业。这个例子没有使用Spring Boot,并且按预期工作,我相信这证实了您看到的行为与Spring Boot自动执行作业有关。

最新更新