簡體   English   中英

測試Spring Batch SkipListener

[英]Testing a Spring Batch SkipListener

我通過XML配置了SkipListener,如下所示:

<batch:job id="importPersonsJob" job-repository="jobRepository">
    <batch:step id="importPersonStep">
        <batch:tasklet transaction-manager="transactionManager">
            <batch:chunk reader="personItemReader" writer="personItemWriter"
                commit-interval="5" skip-limit="10">
                <batch:skippable-exception-classes>
                    <batch:include class="java.lang.Throwable" />
                </batch:skippable-exception-classes>
            </batch:chunk>
            <batch:listeners>
                <batch:listener ref="skipListener" />
            </batch:listeners>
        </batch:tasklet>
    </batch:step>
    <batch:listeners>
        <batch:listener ref="authenticationJobListener" />
        <batch:listener ref="jobListener" />
    </batch:listeners>
</batch:job>

我的SkipListener的實現如下所示:

public class SkipListener {
    @OnSkipInRead
    public void log(final Throwable throwable) throws IOException {
        // make something
    }

    @OnSkipInWrite
    public void log(final Object objectToWrite, final Throwable throwable) throws IOException {
        // make something
    }

    // some dependencies
}

我已經問過谷歌如何有效地測試這一點。 可悲的是,它並沒有那么有效。 也許您可以給我一個簡短的提示,以了解如何實現這一目標。

我假設您不在這里進行單元測試。

Spring Batch附帶了一個不錯的測試框架。

將以下內容添加到您的pom.xml中

<dependency>   
 <groupId>org.springframework.batch</groupId>
    <artifactId>spring-batch-test</artifactId>
    <version>${spring.batch.version}</version>
    <scope>test</scope>
</dependency>

您應該添加測試應用程序上下文以使用JobLauncherTestUtils,例如,您的test-context.xml看起來像

<beans>
    <bean class="org.springframework.batch.test.JobLauncherTestUtils">
        <property name="job" ref="importPersonsJob"/>
    </bean> 
</beans>

然后使用彈簧測試junit運行程序並調用JobLauncherTestUtils.launchStep()

 @ContextConfiguration(
         locations=
                 {"classpath:META-INF/application-conext","classpath: test-context.xml"}
    )
    @RunWith(SpringJUnit4ClassRunner.class)
    public class StepWithSkipTest {

        @Autowired
        private JobLauncherTestUtils jobLauncherTestUtils;

        @Test
        public void testStepWithSkip() throws JobInterruptedException{
            JobExecution execution = jobLauncherTestUtils.launchStep("importPersonStep");

        }
    }

現在,測試將僅執行被測步驟。
棘手的部分是對應用程序上下文xml進行分段,以便對其進行測試

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM