Пример модульного тестирования Spring Batch

Пример тестирования Spring Batch Unit

В этом руководстве мы покажем вам, как выполнять модульное тестирование пакетных заданий Spring с помощью фреймворков jUnit и TestNG. Для пакетного задания модульного тестирования объявляетspring-batch-test.jar,@autowired,JobLauncherTestUtils, запускает задание или шаг и подтверждает статус выполнения.

1. Зависимости модульных тестов

В модульном тестировании пакета Spring объявляются следующие зависимости:

pom.xml

    
    
        org.springframework.batch
        spring-batch-core
        2.2.0.RELEASE
    
    
        org.springframework.batch
        spring-batch-infrastructure
        2.2.0.RELEASE
    

    
    
        org.springframework.batch
        spring-batch-test
        2.2.0.RELEASE
    

    
    
        junit
        junit
        4.11
        test
    

    
    
        org.testng
        testng
        6.8.5
        test
    

2. Весенние партии рабочих мест

Простое задание, позже модульное тестирование состояния выполнения.

spring-batch-job.xml

    
    
    
        
        
        
        
    
    

3. Примеры jUnit

Запускает задание и подтверждает статус выполнения.

AppTest.java

package com.example;

import static org.junit.Assert.assertEquals;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;

    @Test
    public void launchJob() throws Exception {

    //testing a job
        JobExecution jobExecution = jobLauncherTestUtils.launchJob();

    //Testing a individual step
        //JobExecution jobExecution = jobLauncherTestUtils.launchStep("step1");

        assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());

    }
}

P.S Assume context.xml is declared all the require Spring batch core components, like jobRepository and etc.

ЭтотJobLauncherTestUtils должен быть объявлен вручную.

test-context.xml



    
    

4. Примеры TestNG

Эквивалентный пример в рамках TestNG.

AppTest2.java

package com.example;

import org.springframework.batch.core.BatchStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.test.JobLauncherTestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.Assert;
import org.testng.annotations.Test;

@ContextConfiguration(locations = {
    "classpath:spring/batch/jobs/spring-batch-job.xml",
    "classpath:spring/batch/config/context.xml",
    "classpath:spring/batch/config/test-context.xml"})
public class AppTest2 extends AbstractTestNGSpringContextTests {

    @Autowired
    private JobLauncherTestUtils jobLauncherTestUtils;


    @Test
    public void launchJob() throws Exception {

        JobExecution jobExecution = jobLauncherTestUtils.launchJob();
        Assert.assertEquals(jobExecution.getStatus(), BatchStatus.COMPLETED);

    }
}

Готово.

Скачать исходный код

Скачать -SpringBatch-UnitTest-Example.zip (83 КБ)