繁体   English   中英

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

[英]How to test Spring Batch step without running entire job

我正在开发一个 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 自动执行作业有关。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM