Рубрики
Без рубрики

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

– Пример модульного тестирования весенней партии

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

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

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

	
	
		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. Весенние пакетные Задания

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

    
    
	
	    
		
		
	    
	
    

3. Примеры JUnit

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

package com.mkyong;

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 Предположим P.S Предположим объявляются все необходимые компоненты ядра Spring batch, такие как JobRepository и т. Д.

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


 
    
    
    

4. Примеры тестирования

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

package com.mkyong;

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);
        
    }
}

Сделано.

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

Рекомендации

  1. JobLauncherTestUtils джавадок
  2. Официальная документация по тестированию агрегатов весенней партии

Оригинал: “https://mkyong.com/spring-batch/spring-batch-unit-test-example/”