【问题标题】:Spring-Batch: Testing Step Scoped StepSpring-Batch:测试步骤范围步骤
【发布时间】:2016-12-07 10:58:54
【问题描述】:

我正在尝试测试 Spring Batch 步骤。我有 2 个场景要测试 1. 使用 tasklet 步骤(步骤范围) 2. Step with ItemReader/ItemWriter (step scope)

我的测试类注释如下

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
@ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ItemReturnsApplication.class)

This is how my test class looks like

@Bean
    JobLauncherTestUtils jobLauncherTestUtils(){
        return new JobLauncherTestUtils();
    }

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

@Test
    public void testLaunchJob() throws Exception{
        JobExecution jobExecution = jobLauncherTestUtils.launchJob(
                new JobParametersBuilder().addString("chunkSize", "3").addString("current_date","2016-11-25").toJobParameters()
        );
        commonAssertions(jobLauncherTestUtils.launchJob());
        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
    }

当我运行测试用例时,该过程失败,因为作业参数没有传递到我的作业中。

我正在寻找在 Spring Batch 中测试 Step Scoped 步骤的正确方法。

谢谢, 开源浏览器

【问题讨论】:

    标签: spring-batch


    【解决方案1】:

    您当前的代码尝试启动和测试作业而不是步骤。根据spring batch documentation关于如何测试单个步骤的说明,一个简单的例子说明如何测试一个tasklet,并在tasklet中注入上下文,更符合如下代码:

    @ContextConfiguration
    @TestExecutionListeners( { DependencyInjectionTestExecutionListener.class,
        StepScopeTestExecutionListener.class })
    @RunWith(SpringJUnit4ClassRunner.class)
    public class StepScopeTestExecutionListenerIntegrationTests {
    
        // This component is defined step-scoped, so it cannot be injected unless
        // a step is active...
        @Autowired
        private YourTaskletClass yourTaskletClass;
    
        public StepExecution getStepExection() {
            StepExecution execution = MetaDataInstanceFactory.createStepExecution();
            execution.getExecutionContext().putString("input.data", "foo,bar,spam");
            return execution;
        }
    
        @Test
        public void testYourTaskletClass() {
            // The tasklet is initialized and some configuration is already set by the MetaDatAInstanceFactory           
            assertNotNull(yourTaskletClass.doSomething());
        }
    
    }
    

    @RunWith(SpringJUnit4ClassRunner.class) 注解仅适用于 Spring Boot 1.4 及更高版本。有关详细信息,请参阅 this 博客。

    要启动单个步骤,请尝试将您的代码调整为:

    @RunWith(SpringJUnit4ClassRunner.class)
    @TestExecutionListeners({DependencyInjectionTestExecutionListener.class, StepScopeTestExecutionListener.class})
    @ContextConfiguration(loader = SpringApplicationContextLoader.class, classes = ItemReturnsApplication.class)    
    public class StepIntegrationTest {
    
        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;
    
        @Test
        public void testLaunchJob() throws Exception{
           JobExecution jobExecution = jobLauncherTestUtils.launchStep("nameOfYourStep");
    
        } 
    }
    

    【讨论】:

    • 嗨 Sander,我将 spring-boot 更新到 1.4.1 并按照您的建议调整了测试用例。我试图执行一个不需要作业参数的步骤,观察结果是,整个作业开始并查找作业参数并在作业查找作业参数的位置失败......这是预期的行为...... .
    猜你喜欢
    • 2018-09-27
    • 2019-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-05
    • 2019-12-11
    • 2023-03-13
    相关资源
    最近更新 更多