java - 如何在不运行整个作业的情况下测试 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 自动执行作业有关。
推荐阅读
- r - 如何将字符串转换为允许使用二元运算符的“公式”
- node.js - await 不等待函数结束
- openshift - 无法拉取映像“image-registry.openshift-image-registry.svc:5000/...”OpenShift CRC
- python - Flyte 0.16.2:加载 Blob 时出错 - 如何让 Types.Blob.fetch() 在任务修饰函数中工作?
- flutter - 当它向我显示错误时,我该如何处理?
- javascript - 将点击事件应用于具有类的元素
- sql - SQL - 试图得到不存在的东西
- django - 如何使用 django-filters 对基于类的列表视图进行分页
- java - 通过重新启动应用程序 Gson 枚举松散值
- scala - 在使用 scala spark 写入 Kafka 主题之前为 Dataframe 定义模式