首页 > 解决方案 > 如何在不运行整个作业的情况下测试 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());
    }

但是,当我运行这个测试时,它会从头开始工作,同时运行另一个步骤,而不是只运行我想要测试的这个步骤。我无法找到任何解决方案。

标签: javaspringunit-testingjunitspring-batch

解决方案


您可能看到的是,当您运行测试时,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 自动执行作业有关。


推荐阅读