Домой java Пример тестового модуля Spring Batch

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

255
0

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

1. Зависимости модульного тестирования

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

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. Весенние периодические работы

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

весенне-периодического job.xml











3. Примеры jUnit

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

ЧИТАТЬ ТАКЖЕ:  Пример TestNG Hello World

AppTest.java


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 {

// тестируем работу
JobExecution jobExecution = jobLauncherTestUtils.launchJob();

// Тестирование отдельного шага
// JobExecution jobExecution = jobLauncherTestUtils.launchStep ("step1");

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

}
}

PS Предположим context.xml объявляются все необходимые компоненты ядра пакета Spring, такие как jobRepository и т. д.

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

Тест-context.xml








4. Примеры TestNG

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

AppTest2.java


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

}
}

Готово.

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

Загрузить его — SpringBatch-UnitTest-Example.zip (83 КБ)

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

  1. JobLauncherTestUtils JavaDoc
  2. Spring Batch Unit Testing официальная документация

JUnit пружина партии TestNG тестовый модуль

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

0.00 (0%) 0 votes

ОСТАВЬТЕ ОТВЕТ

Пожалуйста, введите ваш комментарий!
пожалуйста, введите ваше имя здесь